How do I get my current location in Google Maps API V2

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 19:36:56

问题


I'm creating an app where the user needs to see his/her geo location with getMyLocation(), but this is returning null. Is there any solution for this, because I did read that the getMyLocation() method is always returning null. I'm new to Google Maps, so any help will be appreciated!

Code:

GoogleMap map = ((SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
Location myLocation = map.getMyLocation();

if( myLocation != null ){
    helper.showToast( "Latitude: " + myLocation.getLatitude() + "\nLongitude: " + myLocation.getLongitude() );
}

回答1:


please check the sample code of the Google Maps Android API v2 , Using this you will solve your problem.

Sample Code




回答2:


private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            mMap.setMyLocationEnabled(true);
            // Check if we were successful in obtaining the map.
            if (mMap != null) {


             mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

           @Override
           public void onMyLocationChange(Location arg0) {
            // TODO Auto-generated method stub

             mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
           }
          });

            }
        }
    }

call this function in on create function




回答3:


I don't like new google maps API because of their limited and defects. In your case I think the best way is to use LocationManager and LocationListener classes. You'll get location, when your phone will find satellites and then you can add the marker on the map. Also you can use getLastKnownLocation() method if you need to get location quickly. I think google maps getMyLocation() method return null because the phone don't have much time to know it's location, when app starts up. And in Google Maps API v2 there is no some kind of onLocationChanged() method. I don't like this API.




回答4:


I have done this way:

private GoogleMap mGoogleMap;
private SupportMapFragment mMapFragment;

on onCreate():

mMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mGoogleMap = mMapFragment.getMap();

mGoogleMap.setOnMyLocationChangeListener(myLocationChangeListener);

Now add MyLocationChangeListener:

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
      Log.i(TAG, "Latitude: "+String.valueOf(location.getLatitude())+" - Longitude: "+String.valueOf(location.getLongitude()));
   }
};

Hope this will help you.




回答5:


map.getMyLocation() return null becouse there is no location on onCreate();

the first time the map get position is after the first onCameraChange() event.

so use

map.setOnCameraChangeListener( new ...)

on your map and do your implementation after the first time occurred



来源:https://stackoverflow.com/questions/14437801/how-do-i-get-my-current-location-in-google-maps-api-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!