此笔记大多数内容来源于高德地图api网站.
1. 向mainifest中添加<meta>时注意添加在<application>,这个低级错误曾经犯过 0.0
2. 添加如下权限:
<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_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" />
3. 定位代码:
private LocationManagerProxy locationManagerProxy;
private TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
initView();
}
private void init() {
locationManagerProxy = LocationManagerProxy.getInstance(this);
locationManagerProxy.setGpsEnable(false);
<span style="white-space:pre"> </span>// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
<span style="white-space:pre"> </span>// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用removeUpdates()方法来取消定<span style="white-space:pre"> </span>//位请求
<span style="white-space:pre"> </span>// 在定位结束后,在合适的生命周期调用destroy()方法
<span style="white-space:pre"> </span>// 其中如果间隔时间为-1,则定位只定一次,
<span style="white-space:pre"> </span>// 在单次定位情况下,定位无论成功与否,都无需调用removeUpdates()方法移除请求,定位sdk内部会移除
locationManagerProxy.requestLocationData(LocationProviderProxy.AMapNetwork,2000,15,this);
}
private void initView() { <pre name="code" class="html"><span style="white-space:pre"> </span>tv_Address <span style="font-family: Arial, Helvetica, sans-serif;">= (TextView) findViewById(R.id.</span><span style="font-family: Arial, Helvetica, sans-serif;">tv_Address</span><span style="font-family: Arial, Helvetica, sans-serif;">);</span>
} @Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null && aMapLocation.getAMapException().getErrorCode()
== 0){ <span style="white-space:pre"> </span>tv_Address<span style="font-family: Arial, Helvetica, sans-serif;">.setText(aMapLocation.getAddress());</span>
} } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void
onProviderDisabled(String provider) { }
@Override
protected void onPause() {
super.onPause();
// 移除定位请求
locationManagerProxy.removeUpdates(this);
// 销毁定位
locationManagerProxy.destroy();
}
来源:CSDN
作者:yuzhzh
链接:https://blog.csdn.net/yuzhzh/article/details/46662473