javascript - Google Maps API using jQuery Plugin: How to get a marker's latitude and longitude on click? - Stack Overflo

I am using a plugin for Google Maps API V3. I use it to put a marker on the map and allow the user to d

I am using a plugin for Google Maps API V3. I use it to put a marker on the map and allow the user to drag the marker to a spot on the map. I want to get the lat,lng of the marker on click.

goMap Plugin Page: .php

This function that sets up the marker in the first place.

$("#map").goMap({ 
   zoom: 16,
   maptype: 'ROADMAP',
   navigationControl: false, 
   mapTypeControl: false, 
   scrollwheel: true, 
   disableDoubleClickZoom: false,
   markers: [{  
       draggable:true,
       latitude: data.lat, 
       longitude: data.lng, 
       icon:'./images/pink.png'
  }] 
});           

I tried calling the native getLat() method on goMap(), but I don't think I was doing that right. Any help is much appreciated.

I am using a plugin for Google Maps API V3. I use it to put a marker on the map and allow the user to drag the marker to a spot on the map. I want to get the lat,lng of the marker on click.

goMap Plugin Page: http://www.pittss.lv/jquery/gomap/index.php

This function that sets up the marker in the first place.

$("#map").goMap({ 
   zoom: 16,
   maptype: 'ROADMAP',
   navigationControl: false, 
   mapTypeControl: false, 
   scrollwheel: true, 
   disableDoubleClickZoom: false,
   markers: [{  
       draggable:true,
       latitude: data.lat, 
       longitude: data.lng, 
       icon:'./images/pink.png'
  }] 
});           

I tried calling the native getLat() method on goMap(), but I don't think I was doing that right. Any help is much appreciated.

Share Improve this question edited Dec 30, 2013 at 8:09 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Dec 22, 2010 at 18:27 JCamJCam 6143 gold badges9 silver badges16 bronze badges 1
  • 1 I was looking for plugin like goMap this question point me to right direction. Thanks. – Daniel Skowroński Commented Mar 10, 2012 at 2:39
Add a ment  | 

2 Answers 2

Reset to default 4

After a bit of sleuthing, it looks like it uses jQuery's data() function to bind data to the map. You can get to the data through $('#map').data(), which has information on markers.

Each marker has an id, and you can get the marker's id through the array $.goMap.markers. Note that $.goMap.markers only contains strings that are IDs and not the markers themselves.

You'll need to use that array to find the id you want (or you might know it ahead of time) and then call $('#map').data()['MARKER_ID'] to get the marker object. The marker has a few properties, including title, visible, icon, id, and position.

We care about position, which has two properties, wa and ya. wa seems to be latitude, while ya seems to be longitude. So $('#map').data()['MARKER_ID'].position.wa will get you the latitude, for example. It seems some markers have a latitude and longitude property, not sure why that's not always there (from my brief testing at least), but you could try $('#map').data()['MARKER_ID'].latitude instead.

Hope this helps.

I don't know why, but the props names changing often with the time... Seems like the best solution is get the key names and use to know the lat & lng.

So, if you want to get the coords after a drag, for example, you'll use:

$.goMap.createListener({type:'marker', marker:'MARKER_ID'}, 'dragend', function() {

    var i_prop = 2;
    var map_data_marker = $('#map').data()['MARKER_ID'].position;
    var map_data_marker_keys = [];

    for (var key in map_data_marker)
    {
        if (i_prop--)
        {
            if (map_data_marker.hasOwnProperty(key))
            {
                map_data_marker_keys.push(key);
            }
        }
        else
        {
            break;
        }
    }

    var lat_lng = map_data_marker[map_data_marker_keys[0]] + ', ' + map_data_marker[map_data_marker_keys[1]];

}); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745557655a4632916.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信