Search code examples
cordovagoogle-mapsgoogle-maps-api-3google-maps-sdk-ios

GoogleMaps SDK Cordova distance in pixel?


I am using the google maps sdk on cordova. I have the position in lat/lng of the top-left corner of my screen and the position of a point in pixel screen. I would like to find the position of this point in lat/lng.

For this, i'd like to know the conversion of 1px in lat and 1px in lng according to the zoom map level.

Do you know how to do that ? Thank you a lot.

UPDATE CODE :

    $("#icon").draggable({
             helper : 'clone',
        stop:function(event,ui) {
            var wrapper = $("#container-emote").offset();
            var borderLeft = parseInt($("#container-emote").css("border-left-width"),10);
            var borderTop = parseInt($("#container-emote").css("border-top-width"),10);
            var pos = ui.helper.offset();
            $scope.createEmote(pos.left - wrapper.left - borderLeft + 40, -(pos.top - wrapper.top - borderTop)+185,2 );
        }
        });

$scope.createEmote = function(a,b,c) {
                var height = screen.height;
                b = height - b;
                map.fromPointToLatLng([a, b], function (point) {
                alert('lat' + point[0] + ' lng:' + point[1]);
            });
        };

SOLUTION :

Well in fact a and b where NaN at the launch, here is the final :

$scope.createEmote = function(a,b,c) {
                var height = screen.height;
                b = height - b;
                if (!isNaN(a) && !isNaN(b)){
                        map.fromPointToLatLng([a , b], function (point) {
                        console.log('mousedown lat' + point[0] + ' lng:' + point[1]);
                    });
                }
        };

Solution

  • I thought this was a duplicate of the earlier question I linked to in the comments. However since you're using Cordova, that changes it a bit.

    If you're using this Cordova Google Maps plugin, then have a look at fromPointToLatLng() described here:

    https://github.com/mapsplugin/cordova-plugin-googlemaps/wiki/Map

    https://github.com/mapsplugin/cordova-plugin-googlemaps/wiki/Map.fromPointToLatLng