这次比较顺,顺着代码来就没出错,唯一就是显示定位自己的位置在地图上的那个点有误差,好几千米呢,才知道这是战略
有个坐标转换
直接贴代码,大部分不变:
package com.example.baidumaptest;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.baidu.mapapi.utils.CoordinateConverter.CoordType;
public class MainActivity extends Activity {
private MapView mapView;
private BaiduMap baiduMap;
private LocationManager locationManager;
private String provider;
private boolean isFirstLocate = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.map_view);
baiduMap = mapView.getMap();
baiduMap.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providerList = locationManager.getProviders(true);
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
Toast.makeText(this, "No location provider to use",
Toast.LENGTH_SHORT).show();
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
navigateTo(location);
}
locationManager.requestLocationUpdates(provider, 1000, 1,
locationListener);
}
private void navigateTo(Location location) {
if (isFirstLocate) {
LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
// 将GPS设备采集的原始GPS坐标转换成百度坐标
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.GPS);
// sourceLatLng待转换坐标
converter.coord(ll);
LatLng desLatLng = converter.convert();
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(desLatLng);
baiduMap.animateMapStatus(update);
update = MapStatusUpdateFactory.zoomTo(16f);
baiduMap.animateMapStatus(update);
isFirstLocate = false;
}
CoordinateConverter converter = new CoordinateConverter();
converter.from(CoordType.GPS);
// sourceLatLng待转换坐标
converter.coord(new LatLng(location.getLatitude(),location.getLongitude()));
LatLng desLatLng = converter.convert();
MyLocationData.Builder locationBuilder=new MyLocationData.Builder();
locationBuilder.latitude(desLatLng.latitude);
locationBuilder.longitude(desLatLng.longitude);
MyLocationData locationData=locationBuilder.build();
baiduMap.setMyLocationData(locationData);
}
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
navigateTo(location);
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
baiduMap.setMyLocationEnabled(false);
mapView.onDestroy();
if (locationManager != null) {
locationManager.removeUpdates(locationListener);
}
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
}
差不多就出来了
我的故事讲到了这。。。