Akshat Agarwal
Akshat Agarwal

Reputation: 2847

How to get users current location coordinates using google maps API and not location manager

Using google maps API v2, I know that myMap.setMyLocationEnabled(true); will let me show the users location on the google map, however I also want the coordinates of this location to be shown underneath the google map. I dont want to use

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

since this looks like an overkill. We already have the location of the user (the on pin pointed on the map), now I just need its coordinate values. How do I get that? I cant find some property such as myLocation.getCurrentCoordinates()

Upvotes: 3

Views: 18275

Answers (6)

Mario
Mario

Reputation: 1

Too late but for the new ones here:

GoogleMap gMap;

double lat = gMap.getCameraPosition().target.latitude;

double lng = gMap.getCameraPosition().target.longitude);

float zoom = gMap.getCameraPosition().zoom);

You get the absolute CAMERA POSITION.

Upvotes: 0

iamshravan
iamshravan

Reputation: 1

//this might help u out

    enter code here
ar initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();

function initialize() {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  }
  // Browser doesn't support Geolocation
  else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }

  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);
  }
}

Upvotes: 0

Akshat Agarwal
Akshat Agarwal

Reputation: 2847

I ended up using LocationClient to get my current location and relying upon getLastLocation()

private LocationClient locationClient;
private Location myLocation = null;
locationClient = new LocationClient(this, this, this);
locationClient.connect();

    @Override
public void onConnected(Bundle arg0) {
    myLocation = locationClient.getLastLocation();
}

This might not always be accurate, however I only needed it once and didnt want to use locationChangedListeners.

Upvotes: 1

Shailendra Madda
Shailendra Madda

Reputation: 21531

Try this too:

GPSTracker tracker = new GPSTracker(this);
    if (tracker.canGetLocation() == false) {
        tracker.showSettingsAlert();
    } else {
        latitude = tracker.getLatitude();
        longitude = tracker.getLongitude();
    }

Upvotes: 0

Boban S.
Boban S.

Reputation: 1662

You can use Google play services for getting user location. http://developer.android.com/training/location/retrieve-current.html

Location is more accurate than location provided with GPS on the device.

Upvotes: 0

Rafael T
Rafael T

Reputation: 15679

try to use:

myMap.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {

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

            }
        });

You will recieve the Location everytime it changes. If you want it only on purpose you can use

Location location = myMap.getMyLocation()

Upvotes: 3

Related Questions