Google Maps API using jQuery Plugin: how do I get the latitude and longitude of a click marker?

I am using the plugin for Google Maps API V3. I use it to place a marker on a map and allow the user to drag the marker onto a spot on the map. I want lat, lng marker pressed.

GoMap plugin page: http://www.pittss.lv/jquery/gomap/index.php

This is the function that sets the marker first.

$("#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 in goMap (), but I don't think I was doing it right. Any help is greatly appreciated.

+3
source share
2 answers

After a little bit of tracking, it looks like it is using the jQuery data() function to bind data to the map. You can go to the data via $('#map').data() , which contains marker information.

Each marker has an identifier, and you can get the identifier of the marker through the $.goMap.markers . Note that $.goMap.markers only contains strings that are identifiers, not the markers themselves.

You will need to use this array to find the identifier you need (or you may know it ahead of time), and then call $('#map').data()['MARKER_ID'] to get the marker object. The marker has several properties, including title , visible , icon , id and position .

We care about position , which has two properties: wa and ya . wa seems to be latitude, and ya seems to be longitude. So $('#map').data()['MARKER_ID'].position.wa will give you latitude, for example. It seems that some markers have the latitude and longitude properties, but are not sure why this does not always exist (at least in my short testing), but you can try instead of $('#map').data()['MARKER_ID'].latitude .

Hope this helps.

+4
source

I don’t know why, but props often change over time ... It seems that the best solution is to get the key names and use the lat and lng icons.

So, if you want to get the coordinates after drag and drop, for example, you will 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]]; }); 
+1
source

All Articles