private AMap amap; //高德地图Api(就是高德地图)
private MapView mapview;
private OnLocationChangedListener changedListener;
private LocationManagerProxy managerProxy;//折线的关键类为 Polyline
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapview=(MapView)findViewById(R.id.map);
mapview.onCreate(savedInstanceState);
init();
}
private void init(){
if(amap==null){
amap=mapview.getMap();
setUpMap();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mapview.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mapview.onPause();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mapview.onDestroy();
}
private void setUpMap(){
amap.setLocationSource(this);//定位监听
amap.getUiSettings().setMyLocationButtonEnabled(true);
amap.setMyLocationEnabled(true);
amap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);
}
@Override
public void onLocationChanged(Location location) {
// 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(changedListener!=null&&arg0!=null){
if(arg0.getAMapException().getErrorCode()==0){
changedListener.onLocationChanged(arg0);
}
}
}
//激活定位
@Override
public void activate(OnLocationChangedListener arg0) {
// TODO Auto-generated method stub
changedListener=arg0;
if(managerProxy==null){
managerProxy=LocationManagerProxy.getInstance(this);
managerProxy.requestLocationData(LocationProviderProxy.AMapNetwork, 60*1000, 10, this);
}
}
//停止定位
@Override
public void deactivate() {
// TODO Auto-generated method stub
changedListener=null;
if(managerProxy!=null){
managerProxy.removeUpdates(this);
managerProxy.destroy();
}
managerProxy=null;
}
}
//xml文件
<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="1a1786d95eee2887a266d2c65778d52e" />s