7463

how to get the location(lat/lng) on google maps v3 from the location(x,y)

Question:

i have the (x,y)

(event.pageX-$("#map_canvas").offset().left,event.pageY-$("#map_canvas").offset().top)

but how to get the (lat,lng)

thanks

Answer1:

In your initialize function for a map define:

var overlay; overlay = new google.maps.OverlayView(); overlay.draw = function() {}; overlay.setMap(map);

Next when you want to get the location run:

var point=new google.maps.Point(x,y); var location=$overlay.getProjection().fromContainerPixelToLatLng(point);

Best K

Answer2:

Enter left and top pixel offset (x,y) and it returns a google.maps.LatLng

function PixelToLatLng(x,y){ var overlay = new google.maps.OverlayView() overlay.setMap(map); var LatLng = overlay.getProjection().fromContainerPixelToLatLng(new google.maps.Point(x,y)); overlay.setMap(null); delete overlay; return LatLng; }

Surprised this isn't in the google.maps.Map object.

Answer3:

Here's a code that doesn't rely on OverlayView(). This method makes use of the built in fromPointToLatLng() method in the maps API.

function pixelToLatlng(xcoor, ycoor) { var ne = map.getBounds().getNorthEast(); var sw = map.getBounds().getSouthWest(); var projection = map.getProjection(); var topRight = projection.fromLatLngToPoint(ne); var bottomLeft = projection.fromLatLngToPoint(sw); var scale = 1 << map.getZoom(); var newLatlng = projection.fromPointToLatLng(new google.maps.Point(xcoor / scale + bottomLeft.x, ycoor / scale + topRight.y)); return newLatlng; };

Source: <a href="http://magicalrosebud.com/how-to-use-googlemaps-api-frompointtolatlng/" rel="nofollow">http://magicalrosebud.com/how-to-use-googlemaps-api-frompointtolatlng/</a>

Here's a fiddle: <a href="http://jsfiddle.net/mhaq865o/5/" rel="nofollow">http://jsfiddle.net/mhaq865o/5/</a>

Recommend

  • Local Schedule Notification react native
  • Ignore uppercase, downcase and accent in search
  • How to replace %0A with br from querystring?
  • Fastest way to convert IEnumerable to List in C#
  • Fitbit oauth registration
  • Could there be any performance difference between writing the same code (for the layout) in XML or j
  • How to Include CSS style when converting svg to png
  • Salesforce consuming XML and display data in Visualforce report
  • Xamarin.Forms: How To Populate A Pie Chart From Web API Data?
  • Train two models concurrently
  • Windows 8 Flexboxes - Nesting Flexboxes with overflow enabled
  • Tableview make specific cell or row editable
  • Microsoft bot framework webchat C#
  • How to create wsdl from xsd
  • How to convert SVG to jVectorMap format
  • Parallelization via JDBC - Pyspark - How does parallelization work using JDBC?
  • Magento Layered Navigation block. Move to center
  • Calculate savings percentage for house down payment in 36 months
  • Is there a way to run c# forms application without showing a gui window (like a console application)
  • sec:authorize not being evaluated on spring-boot project
  • How to smoothly connect two signals in matlab [closed]
  • Python sum values in tuple in a list in a dictionary?
  • Wicket - getting body of markup element
  • Tkinter tkMessageBox disables Tkinter key bindings
  • dmtracedump doesn't work, HELP!
  • Optimization of optim() in R ( L-BFGS-B needs finite values of 'fn')
  • .Net core Hosted Services guaranteed to complete
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • ASP.NET MVC 2 actions for the same route?
  • Add font awesome icon to custom add to cart button in Woocommerce 3
  • how to read to huge file into buffer
  • How to handle div that is created dynamically in a table
  • Google App Engine backend servlet not responding
  • What is the best way to cache and reuse immutable singleton objects in Java?