android下利用高德地图获取经纬度等定位信息

第 1 步,配置AndroidManifest.xml


首先,声明Service组件

请在application标签中声明service组件,每个app拥有自己单独的定位service。

1
"com.amap.api.location.APSService" >

然后,声明权限

Android 6.0及以上系统可以参考Android 6.0权限说明章节

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"android.permission.ACCESS_COARSE_LOCATION" >
"android.permission.ACCESS_FINE_LOCATION" >
"android.permission.ACCESS_NETWORK_STATE" >
"android.permission.ACCESS_WIFI_STATE" >
"android.permission.CHANGE_WIFI_STATE" >
"android.permission.INTERNET" >
"android.permission.READ_PHONE_STATE" >
"android.permission.WRITE_EXTERNAL_STORAGE" >

设置高德地图的key

设置高德地图的key
"com.amap.api.v2.apikey" android:value= "key" > //开发者申请的key      
             


接下来写个demo代码
package com.aeckj.fuelgas;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class LocationTestActivity extends Activity {
	
	//声明AMapLocationClient类对象
	public AMapLocationClient mLocationClient = null;
	//声明定位回调监听器
	public AMapLocationListener mLocationListener = new AMapLocationListener() {
		
		@Override
		public void onLocationChanged(AMapLocation amapLocation) {
			// TODO Auto-generated method stub
			if (amapLocation != null) {
			    if (amapLocation.getErrorCode() == 0) {
			    	//可在其中解析amapLocation获取相应内容。
			    	double locationType = amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
			    	double latitude = amapLocation.getLatitude();//获取纬度
			    	Log.e("Amap==经度:纬度", "locationType:"+locationType+",latitude:"+latitude);
			    }else {
			    //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
			    Log.e("AmapError","location Error, ErrCode:"
			        + amapLocation.getErrorCode() + ", errInfo:"
			        + amapLocation.getErrorInfo());
			    }
			}
		}
	};
	
	//声明AMapLocationClientOption对象
	public AMapLocationClientOption mLocationOption = null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//初始化定位
		mLocationClient = new AMapLocationClient(getApplicationContext());
		//设置定位回调监听
		mLocationClient.setLocationListener(mLocationListener);
		
		//初始化AMapLocationClientOption对象
		mLocationOption = new AMapLocationClientOption();
		//设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。
		mLocationOption.setLocationMode(AMapLocationMode.Battery_Saving);
		
		
		//给定位客户端对象设置定位参数
		mLocationClient.setLocationOption(mLocationOption);
		//启动定位
		mLocationClient.startLocation();
		
		/**
		 * 获取一次定位
		 */
		//该方法默认为false,true表示只定位一次
		mLocationOption.setOnceLocation(true);
		 
		
		 

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

}



你可能感兴趣的:(Android开发)