定位

高德定位

要想实现定位功能  首先需要从高德的官网申请一个key 下载 AMap_Location_v1.4.1_20150917.jar 包 把该包放在所建android 项目的bins文件里面。

在AndroidManifest.xml 进行配置 把下面代码加入application 中
  <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="83c4f8b7b8f4ef6b80e6d01dfbbc1d29" />

   83c4f8b7b8f4ef6b80e6d01dfbbc1d29 是你申请的key


AndroidManifest.xml 中的权限配置代码
 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />


布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    
     <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
       
        />

</LinearLayout>

java代码
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.example.map.R;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;


public class Location extends Activity implements AMapLocationListener {
	LocationManagerProxy mLocationManagerProxy;
	private TextView tv;


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);


	//	requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.location);
		tv = (TextView) findViewById(R.id.tv);


		// 初始化定位对象
		mLocationManagerProxy = LocationManagerProxy.getInstance(this);


		// 定位注册
		// 第一个参数是定位类型:高德定位,混合类型包括GPS和网络定位,哪个先返回就采用哪个定位
		// ,同时返回的话优先GPS定位。第二个参数是定位周期
		// 单位毫秒。第三个参数是对GPS有用
		// 移动多大距离从新定位,
		mLocationManagerProxy.requestLocationData(
				LocationProviderProxy.AMapNetwork, 2000, 15, this);


	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();


		mLocationManagerProxy.destroy();
	}


	@Override
	public void onLocationChanged(android.location.Location arg0) {
		// TODO Auto-generated method stub


	}


	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub


	}


	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub


	}


	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub


	}


	@Override
	public void onLocationChanged(AMapLocation arg0) {
		// 定位回调
		if (arg0 != null && arg0.getAMapException().getErrorCode() == 0) {
			Log.d("hello world", arg0.toString());
			tv.setText(arg0.toString());
		}


	}


}







  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值