public class MainActivity extends Activity {
private MapView mapView;
private LocationManager locationManager;
private String provider;
private Location location;
private BaiduMap baiduMap;
private boolean isFirstLocate=true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView=(MapView)findViewById(R.id.map1_view);
baiduMap=mapView.getMap();
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, "请打开GPS或者联网!", Toast.LENGTH_LONG).show();
return;
}
location=locationManager.getLastKnownLocation(provider);
if(location !=null){
Log.d("map", "ffff");
showLocation(location);
}
locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
}
LocationListener locationListener=new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location arg0) {
if(location!=null){
showLocation(location);
}
}
};
protected void showLocation(Location location){
if(isFirstLocate){
LatLng latLng=new LatLng(location.getLatitude(), location.getAltitude());
MapStatusUpdate update=MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.animateMapStatus(update);
update=MapStatusUpdateFactory.zoomTo(16f);
baiduMap.animateMapStatus(update);
isFirstLocate=false;
}
Log.d("map", "ggggg");
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mapView.onDestroy();
if(location!=null){
locationManager.removeUpdates(locationListener);
}
}
为什么我一直无法地位到我所在地,还有就是我的key什么都没错,因为我地图能显示默认的北京地图。