Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  137] [ 2]  / answers: 1 / hits: 15780  / 11 Years ago, mon, november 18, 2013, 12:00:00

I currently have the following code:



jQuery.each(json,function(index,value){
var id =json[index][id];
var location =json[index][location];
var tel =json[index][tel];
var lat=json[index][lat];
var lng=json[index][lng];

marker = new google.maps.Marker({
id:id,
position: new google.maps.LatLng (lat, lng),
icon: {
path: google.maps.SymbolPath.CIRCLE,
fillOpacity: 1,
fillColor: '#002664',
strokeWeight: 1,
strokeColor: '#FFFFFF',
scale: 5 //pixels
},
map: map
});

google.maps.event.addListener(marker, 'click', function() {
jQuery('.office-information').slideUp(300);
if(!jQuery('#office-info-'+id).is(':visible')){
jQuery('#office-info-'+id).slideDown(300);
}
});

google.maps.event.addListener(marker, 'click', function() {
});

jQuery('.get-direction').on('click', function(){
var markerID = this.id.replace( /[^d.]/g,'');
console.log(marker);
});

});


The problem area is:



jQuery('.get-direction').on('click', function(){
var markerID = this.id.replace( /[^d.]/g,'');
console.log(marker);
});


basically, when I click on a link with a class of get-direction i grab its id and strip everything but the numbers from it, that leaves me with the markers id. But How do I get a marker by id? I actually just need the latitude and longitude from the marker.



Any Help Greatly Appreciated.


More From » jquery

 Answers
205

You can create a global associative array and store all your markers in there.
E.g. like this



var arrMarkers = {};
jQuery.each(json,function(index,value){
...
marker = new google.maps.Marker({
...
});
arrMarkers[id] = marker;
...
jQuery('.get-direction').on('click', function(){
var markerID = this.id.replace( /[^d.]/g,'');
console.log(arrMarkers[markerID]);
});
}

[#74223] Saturday, November 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;