How to use fromPointToLatLng on google maps v3

This is my code:

var latlng = new google.maps.LatLng(-34.397, 150.644); var myOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); google.maps.event.addListener(map,'projection_changed', function () { var proj = map.getProjection(); ltBound = proj.fromPointToLatLng(new google.maps.Point(0,100)) rbBound = proj.fromPointToLatLng(new google.maps.Point(100,200)) console.log(ltBound,rbBound) }); 

I want to make a node on Google Maps, but I cannot use fromPointToLatLng on the right path. What can I do?

+4
source share
2 answers

This is how I use the fromPointToLatLng () method. First you need to find the Lat / Lng of the top left point on the map, and then calculate the offset from there.

 var pixelToLatlng = function(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; }; 

Try the fiddle here: http://jsfiddle.net/mhaq865o/5/

You can use fromPointToLatLng () method or OverlayView method as described here http://magicalrosebud.com/how-to-use-googlemaps-api-frompointtolatlng/

+5
source

The fromPointToLatLng and fromLatLngToPoint in Projection translate points in the 256x256 coordinate system and real-world coordinates.

The entire Google map has 256x256 pixels when you are at zoom level 0. Try to scale and display the point that you get when you click on the map:

 google.maps.event.addListener(map, 'click', function(event) { var proj = map.getProjection(); console.log('latLng: ' + event.latLng); console.log('Point: ' + proj.fromLatLngToPoint(event.latLng)); }); 

I think you only need to know / use this if you want to calculate the zoom level or do something similar, where you need to know the relationship between real-world coordinates and screen pixels.

You are asking about adding a "node" to the map. If you are thinking about what documentation calls "markers", see the section on markers .

+1
source

All Articles