0

I wanted to ask if there's a way to convert x, y coordinates on a certain place in the browser into google maps coordinates without adding a click listener in javascript ?

I already have the browser's pixel coordinates but I need to convert that into geographical form for later usage ; the pixel coordinates are not generated by user click.

UPDATE : I have found a very useful question that is asked before mine : Create google maps markers from pixel xy coordinates

Everything works in it except for getting the projection from an overlay variable.

My script :

function initialize()
{

   //Not related to the problem ; 

    latlng_local = markerData.split('@');
    lat = latlng_local[0];

    lng = latlng_local[1];
    latlng = new google.maps.LatLng(lat, lng);


    var mapOptions = 

    {
    center: new google.maps.LatLng(lat, lng),
    zoom: 8
    };

    map = new google.maps.Map(document.getElementById("map"),
                              mapOptions);    


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


//My question starts from here
    google.maps.event.addListenerOnce(map,"idle", handleRandCoord() );


}


function handleRandCoord()
{    alert('xx');
    xhRequest.open("GET", "http://sample", false);
    xhRequest.send(null);



    var proximityList = new Array();
    proximityList = markerData.split('@');


//Problem is in here ; the script stops once I call overlay.getProjection()
    var pixelLatLng = overlay.getProjection().fromDivPixelToLatLng( new google.maps.Point(200,200) );
    alert("ss");





    var populationOptions = {
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35,
    map: map,
    };
    // Add the circle for this city to the map.
   var cityCircle = new google.maps.Circle(populationOptions);
    cityCircle.setCenter(pixelLatLng);
    cityCircle.setRadius(proximityList[1] );
    cityCircle.setMap(map);



}
Community
  • 1
  • 1
  • So you have the location when the page loads? ie its not entered by the user? – Giovanni Mar 25 '14 at 17:21
  • Yes. It is randomly generated within a bound of (800 X 600) – user1405097 Mar 25 '14 at 17:25
  • does this help? http://stackoverflow.com/questions/7020771/convert-lat-long-to-pixel-xy-co-ordinates – Giovanni Mar 25 '14 at 17:32
  • Thank you for posting the link, but it is not what I intended. I found what I exactly want here http://stackoverflow.com/questions/21505636/create-google-maps-markers-from-pixel-xy-coordinates . but it doesn't seem to work ; everything works except for the line " var pixelLatLng = overlay.getProjection().fromDivPixelToLatLng(new google.maps.Point(pixel_X,pixel_Y));". – user1405097 Mar 26 '14 at 02:37

0 Answers0