<com.amap.api.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
//地图包、搜索包需要的基础权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
//定位包、导航包需要的额外权限(注:基础权限也需要)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="87c0150fd8a38899af746c1a44efbf1a"
/
package com.example.gaode02;
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.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity implements AMapLocationListener {
//声明变量
private MapView mapView;
private AMap aMap;
private LocationManagerProxy mLocationManagerProxy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManagerProxy = LocationManagerProxy.getInstance(this);
//此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
//注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
//在定位结束后,在合适的生命周期调用destroy()方法
//其中如果间隔时间为-1,则定位只定一次
mLocationManagerProxy.requestLocationData(
LocationProviderProxy.AMapNetwork,-1, 15, this);
mLocationManagerProxy.setGpsEnable(false);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 必须要写
aMap = mapView.getMap();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLocationManagerProxy.destroy();
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
if(arg0 != null && arg0.getAMapException().getErrorCode() == 0){
//获取位置信息
//Double geoLat = arg0.getLatitude();
//Double geoLng = arg0.getLongitude();
Log.e("TGA","地址"+arg0.toString());
Log.e("TGA","地址"+arg0.getAMapException().getErrorCode());
}
}
}
本文介绍如何使用高德地图API实现Android应用中的位置定位功能。通过代码示例展示了必要的地图视图配置及定位权限设置,同时实现了定位监听器以获取实时位置更新。
8万+





