Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call fromLatLngToDivPixel in Google Maps API V3?

I know that method exists and is documented, but I don't know how to get an MapCanvasProjection object.

like image 718
Jader Dias Avatar asked Oct 08 '09 15:10

Jader Dias


People also ask

How do you call Google Maps API?

Click the Navigation Bar on the top left corner and go to APIs & Services > Dashboard. Click “+Enable APIs and Services” at the top of the page. Select “Maps Javascript API” for our case. Hit “Enable.”

How do you get bounds on Google Maps?

getBounds() in Google Maps API v3 But in API v3 you will get “bounds is undefined” error. So to get our latitude and longitude we need to move getBounds(), to some event listener. Description of bounds_changed in documentation is: “This event is fired when the viewport bounds have changed.”

Is Google Maps API no longer free?

You won't be charged until your usage exceeds $200 in a month. Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).


2 Answers

Look at http://qfox.nl/notes/116

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

Ugly indeed. Much easier in v2 - another flaw of google api v3!

like image 108
Tomas Avatar answered Sep 28 '22 05:09

Tomas


I think the easiest way is to ignore Google's desire to make our life harder by removing and hiding useful functions instead of adding new ones, and just to write your own methods that do the same thing.

Here's a version of a function somebody posted somewhere else (I can't find it right now), that worked for me:

fromLatLngToPixel: function (position) {   var scale = Math.pow(2, Map.getZoom());   var proj = Map.getProjection();   var bounds = Map.getBounds();    var nw = proj.fromLatLngToPoint(     new google.maps.LatLng(       bounds.getNorthEast().lat(),       bounds.getSouthWest().lng()     ));   var point = proj.fromLatLngToPoint(position);    return new google.maps.Point(     Math.floor((point.x - nw.x) * scale),     Math.floor((point.y - nw.y) * scale)); }, 

Now you can call it any time and any where you want. I especially needed it for custom context menus, and it does it's job perfectly.

EDIT: I also wrote a reverse function, fromPixelToLatLng that does exactly the opposite. It is simply based on the first one, with some math applied:

fromPixelToLatLng: function (pixel) {   var scale = Math.pow(2, Map.getZoom());   var proj = Map.getProjection();   var bounds = Map.getBounds();    var nw = proj.fromLatLngToPoint(     new google.maps.LatLng(       bounds.getNorthEast().lat(),       bounds.getSouthWest().lng()     ));   var point = new google.maps.Point();    point.x = pixel.x / scale + nw.x;   point.y = pixel.y / scale + nw.y;    return proj.fromPointToLatLng(point); } 
like image 21
Tiborg Avatar answered Sep 28 '22 04:09

Tiborg