Stewie Griffin
Stewie Griffin

Reputation: 9327

how to get coordinates of the center of the viewed area in google maps using Google Maps JavaScript API v3

How to get coordinates using javascript Google Maps JavaScript API v3 of the center of the currently viewed area on google maps? Thanks

Upvotes: 70

Views: 69152

Answers (3)

Victoria
Victoria

Reputation: 1

This was a bug bearer for me. You have to create a pin element in the xml as such if you want a hoover over the map on centre.

//onMapReady
 mMap = googleMap;

//In your listener/ screen code area
 LatLng latlong = mMap.getCameraPosition().target;

HashMap<String, Object> map = new HashMap<>();
     map.put("Latitude", latlong.latitude);
     map.put("Longitude", latlong.longitude);
   //push to your database 

Upvotes: 0

yehyatt
yehyatt

Reputation: 2404

Updated 2018 The below codes are for Google Maps API for Android. In case anyone needs this Script:

double mylat = mGoogleMap.getCameraPosition().target.latitude;               
double mylon = mGoogleMap.getCameraPosition().target.longitude;

Upvotes: 18

Mike Jeffrey
Mike Jeffrey

Reputation: 3209

You can call:

map.getCenter();

to return a LatLng object.

Upvotes: 150

Related Questions