1

I'm using the Bing Maps API...

And I want to get the latitude and longitude on click function. This is my HTML code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=[YOUR_BING_MAPS_KEY]' async defer></script>
</head>
<body>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
    <script type='text/javascript'>
        var map;

        function GetMap() {
            map = new Microsoft.Maps.Map('#myMap', {});
            Microsoft.Maps.Events.addHandler(map, 'click', function () { set_latitudes_and_longitude(map); });
        }

        function set_latitudes_and_longitude(map)
        {
            console.log(map);
            // How can I wright something like
            // map.getLat or map.getLong?

        }
    </script>
</body>
</html>

On console.log(map); I did not find anything referring to latitude and longitude...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
softya
  • 229
  • 7
  • 22
  • The event handler will most likely have an argument that describes the event, including a way to get the coordinates of the click on the map. – Andreas Sep 13 '20 at 09:34
  • [How to get lat/lon of a mouse click with Bing Maps AJAX Control v7](https://stackoverflow.com/questions/8590875/how-to-get-lat-lon-of-a-mouse-click-with-bing-maps-ajax-control-v7) – Andreas Sep 13 '20 at 09:36
  • @Andreas im using v8 not v7 – softya Sep 13 '20 at 12:21
  • Your solution is the same as in the possible dupe I've linked in my comment - which tells me, that you only looked at the text of the link but not the question/answer behind that link... – Andreas Sep 13 '20 at 13:58

1 Answers1

1

I did this and it worked:

<script type='text/javascript'>
    var map;

    function GetMap() {
        map = new Microsoft.Maps.Map('#myMap', {});
        Microsoft.Maps.Events.addHandler(map, 'click', function (e) { set_latitudes_and_longitude(e); });
    }

    function set_latitudes_and_longitude(map)
    {
        console.log(map.location.latitude);
        console.log(map.location.longitude);
        // How can I write something like 
        // map.getLat or map.getLong?
        
    }
</script>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
softya
  • 229
  • 7
  • 22