申请 API Key
为保证服务可以正常使用,您需要登录AMap API网站申请 API Key。每个帐户,最多可以申请10个 Key。Key 申请的具体流程请参照获取密钥。链接地址:http://api.amap.com/key/register
PS:高德定位SDK文档没有百度的详细,API兼容性也有点差强人意
申请完Key之后,整合就很简单啦,这里以最新的V1.0.5版本为例。
1、导入jar文件
将Android_Location_V1.0.5.jar拷贝到 libs 的根目录下
2、设置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.amaploc" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <!-- 定位权限 --> <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" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- 配置Api Key --> <meta-data android:name="com.amap.api.v2.apikey" android:value="540baf255d7ec81c382ef7b714fdfdfe" /> <activity android:name="com.example.amaploc.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
3、使用LocationManagerProxy定位
package com.example.amaploc; import java.util.Date; import android.app.Activity; import android.location.Location; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationListener; import com.amap.api.location.LocationManagerProxy; import com.amap.api.location.LocationProviderProxy; public class MainActivity extends Activity implements OnClickListener, AMapLocationListener { private TextView tv_address; private LocationManagerProxy mAMapLocManager = null; private AMapLocation aMapLocation;// 用于判断定位超时 private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findView(); } private Runnable stop = new Runnable() { @Override public void run() { if (aMapLocation == null) { Toast.makeText(getApplicationContext(), "12秒内还没有定位成功,停止定位", Toast.LENGTH_SHORT).show(); stopLocation();// 销毁掉定位 } } }; private void findView() { Button bt_start = (Button) findViewById(R.id.bt_start); tv_address = (TextView) findViewById(R.id.tv_address); bt_start.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_start: mAMapLocManager = LocationManagerProxy.getInstance(this); /* * mAMapLocManager.setGpsEnable(false);// * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location * API定位采用GPS和网络混合定位方式 * ,第一个参数是定位provider,第二个参数时间最短是5000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者 */ mAMapLocManager.requestLocationUpdates( LocationProviderProxy.AMapNetwork, 5000, 10, this); handler.postDelayed(stop, 12000);// 设置超过12秒还没有定位到就停止定位 break; default: break; } } @Override protected void onPause() { super.onPause(); stopLocation();// 停止定位 } /** * 销毁定位 */ private void stopLocation() { if (mAMapLocManager != null) { mAMapLocManager.removeUpdates(this); mAMapLocManager.destory(); } mAMapLocManager = null; } /** * 此方法已经废弃 */ @Override public void onLocationChanged(Location location) { } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } /** * 混合定位回调函数 */ @Override public void onLocationChanged(AMapLocation location) { if (location != null) { this.aMapLocation = location;// 判断超时机制 double geoLat = location.getLatitude(); double geoLng = location.getLongitude(); String cityCode = ""; String desc = ""; Bundle locBundle = location.getExtras(); if (locBundle != null) { cityCode = locBundle.getString("citycode"); desc = locBundle.getString("desc"); } String str = ("定位成功:(" + geoLng + "," + geoLat + ")" + "\n精 度 :" + location.getAccuracy() + "米" + "\n定位方式:" + location.getProvider() + "\n定位时间:" + new Date(location.getTime()).toLocaleString() + "\n城市编码:" + cityCode + "\n位置描述:" + desc + "\n省:" + location.getProvince() + "\n市:" + location.getCity() + "\n区(县):" + location.getDistrict() + "\n区域编码:" + location .getAdCode()); tv_address.setText(str); } } }
Demo下载地址:http://download.csdn.net/detail/fx_sky/6878091