使用Android Google Map v2获取位置信息

时间:2013-11-13 08:48:47

标签: google-maps-android-api-2

我无法获得我的位置。它总是崩溃。我查了一切。有谁能帮我找到我的位置?

private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    if(isGooglePlay()){
        setContentView(R.layout.map);
        setupmapifneeded();
    }
}

private void setupmapifneeded() {
    if(map==null){

         map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();


    }if(map!=null){

         map.setMyLocationEnabled(true);

         LocationManager mlm = (LocationManager)getSystemService(LOCATION_SERVICE);
         Criteria c = new Criteria();

         String provider = mlm.getBestProvider(new Criteria(), false);

         if(provider == null){

             onProviderDisabled(provider);
         }

         Location mylocation = mlm.getLastKnownLocation(provider);
         if(mylocation != null){
             onLocationChanged(mylocation);
         }   
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

}

private boolean isGooglePlay(){
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if(status == ConnectionResult.SUCCESS){
        return true;

    }else{
        ((Dialog)GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
        //Toast.makeText(this, "nai", Toast.LENGTH_LONG).show();
    }
    return false;

}

@Override
public void onLocationChanged(Location mylocation) {
    double lat = mylocation.getLatitude();
    double longi = mylocation.getLongitude();
    LatLng latlng = new LatLng(lat,longi);

    map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    map.animateCamera(CameraUpdateFactory.zoomTo(10));
}

2 个答案:

答案 0 :(得分:0)

你可以简单地使用

map.setMyLocationEnabled(真)

这会自动在地图上显示您的位置。

答案 1 :(得分:0)

Location location = map.getMyLocation();