2010-04-01 17 views
66

मेरे पास एक ऐसी स्क्रिप्ट है जो एक समय में मार्कर को लूप और जोड़ती है।Google मानचित्र मार्करों में आईडी जोड़ना

मैं और एक जानकारी विंडो के लिए वर्तमान मार्कर पाने के लिए कोशिश कर रहा हूँ और एक बार में केवल एक नक्शे पर 5 मार्कर (जानकारी विंडो के बिना 4 और 1 के साथ)

मैं कैसे प्रत्येक के लिए एक आईडी जोड़ना होगा मार्कर ताकि मैं आवश्यकतानुसार जानकारी विंडो को हटा और बंद कर सकूं।

function codeAddress(address, contentString) { 

var infowindow = new google.maps.InfoWindow({ 
    content: contentString 
}); 

if (geocoder) { 

    geocoder.geocode({ 'address': address}, function(results, status) { 

    if (status == google.maps.GeocoderStatus.OK) { 

     map.setCenter(results[0].geometry.location); 

     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 

     infowindow.open(map,marker); 

     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    } 

}

+0

मैं नहीं था एनके एक मार्कर में एक आईडी जोड़ना संभव है। मुझे लगता है कि आप प्रत्येक मार्कर को एक कस्टम शीर्षक दे सकते हैं और शीर्षक से मेल खाने वाले div टैग को ढूंढ सकते हैं। हालांकि एक आदर्श समाधान नहीं है। –

+0

क्या किसी ने आपके प्रश्न का उत्तर दिया है? – CrazyEnigma

उत्तर

166

जावास्क्रिप्ट एक गतिशील भाषा है। आप इसे ऑब्जेक्ट में ही जोड़ सकते हैं।

var marker = new google.maps.Marker(markerOptions); 
marker.metadata = {type: "point", id: 1}; 

इसके अलावा, क्योंकि सभी v3 ऑब्जेक्ट्स MVCObject() का विस्तार करते हैं। आप उपयोग कर सकते हैं:

var marker = new google.maps.Marker({ 
    map: map, 
    position: position, 

    // Custom Attributes/Data/Key-Values 
    store_id: id, 
    store_address: address, 
    store_type: type 
}); 

:

marker.setValues({type: "point", id: 1}); 
// or 
marker.set("type", "point"); 
marker.set("id", 1); 
var val = marker.get("id"); 
+8

मुझे उत्सुकता है कि, एक बार जब आप मार्कर आईडी देते हैं, तो आप मानचित्र कैनवास के बाहर इन मार्करों तक पहुंचने के बारे में कैसे जाते हैं। $ ('# 1') DoSomething()।; उदाहरण के लिए? – willdanceforfun

+0

यदि 'marker.set (" id ", 1);' काम नहीं करता है, 'marker.set (' id ', 1) का उपयोग करें; '। यह मेरे मामले में काम करता है। –

+0

मुझे संदेश मिल रहा है: marker.get कोई फ़ंक्शन नहीं है। – Rodrigo

1

क्यों एक कैश कि प्रत्येक मार्कर वस्तु और उसका संदर्भ आईडी संग्रहीत करता है का उपयोग नहीं:

इस समारोह मैं मार्कर सेट करने के लिए उपयोग कर रहा हूँ है?

var markerCache= {}; 
var idGen= 0; 

function codeAddress(addr, contentStr){ 
    // create marker 
    // store 
    markerCache[idGen++]= marker; 
} 

संपादित करें: बेशक यह एक न्यूमेरिक इंडेक्स सिस्टम पर निर्भर करता है जो एक सरणी की तरह लंबी संपत्ति प्रदान नहीं करता है। आप निश्चित रूप से ऑब्जेक्ट ऑब्जेक्ट को प्रोटोटाइप कर सकते हैं और ऐसी चीज के लिए लंबाई बना सकते हैं। ओटीओएच, प्रत्येक पते का एक अद्वितीय आईडी मान (एमडी 5, आदि) उत्पन्न करने का तरीका हो सकता है।

3

मेरे पास एक साधारण Location कक्षा है जिसका उपयोग मैं अपने सभी मार्कर से संबंधित चीजों को संभालने के लिए करता हूं। मैं आपके लिए एक कोडर लेने के लिए नीचे अपना कोड पेस्ट कर दूंगा।

अंतिम पंक्ति वास्तव में मार्कर ऑब्जेक्ट्स बनाती है। यह मेरी स्थानों, जो कुछ इस तरह दिखाई के कुछ JSON के माध्यम से लूप:

तुम मेरे स्थान कक्षा में target() विधि को देखें, तो आपको लगता है कि मैं रखने देखेंगे:

{"locationID":"98","name":"Bergqvist Järn","note":null,"type":"retail","address":"Smidesvägen 3","zipcode":"69633","city":"Askersund","country":"Sverige","phone":"0583-120 35","fax":null,"email":null,"url":"www.bergqvist-jb.com","lat":"58.891079","lng":"14.917371","contact":null,"rating":"0","distance":"45.666885421019"} 

यहाँ कोड है infowindow के संदर्भ और संदर्भ के कारण open() और close() कर सकते हैं।

एक लाइव डेमो देखें: http://ww1.arbesko.com/en/locator/ (एक स्वीडिश शहर में प्रकार, स्टॉकहोम की तरह, और एंटर दबाएँ)

var Location = function() { 
    var self = this, 
     args = arguments; 

    self.init.apply(self, args); 
}; 

Location.prototype = { 
    init: function(location, map) { 
     var self = this; 

     for (f in location) { self[f] = location[f]; } 

     self.map = map; 
     self.id = self.locationID; 

     var ratings = ['bronze', 'silver', 'gold'], 
      random = Math.floor(3*Math.random()); 

     self.rating_class = 'blue'; 

     // this is the marker point 
     self.point = new google.maps.LatLng(parseFloat(self.lat), parseFloat(self.lng)); 
     locator.bounds.extend(self.point); 

     // Create the marker for placement on the map 
     self.marker = new google.maps.Marker({ 
      position: self.point, 
      title: self.name, 
      icon: new google.maps.MarkerImage('/wp-content/themes/arbesko/img/locator/'+self.rating_class+'SmallMarker.png'), 
      shadow: new google.maps.MarkerImage(
             '/wp-content/themes/arbesko/img/locator/smallMarkerShadow.png', 
             new google.maps.Size(52, 18), 
             new google.maps.Point(0, 0), 
             new google.maps.Point(19, 14) 
            ) 
     }); 

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

     google.maps.event.addListener(self.marker, 'mouseover', function() { 
      self.sidebarItem().mouseover(); 
     }); 

     google.maps.event.addListener(self.marker, 'mouseout', function() { 
      self.sidebarItem().mouseout(); 
     }); 

     var infocontent = Array(
      '<div class="locationInfo">', 
       '<span class="locName br">'+self.name+'</span>', 
       '<span class="locAddress br">', 
        self.address+'<br/>'+self.zipcode+' '+self.city+' '+self.country, 
       '</span>', 
       '<span class="locContact br">' 
     ); 

     if (self.phone) { 
      infocontent.push('<span class="item br locPhone">'+self.phone+'</span>'); 
     } 

     if (self.url) { 
      infocontent.push('<span class="item br locURL"><a href="http://'+self.url+'">'+self.url+'</a></span>'); 
     } 

     if (self.email) { 
      infocontent.push('<span class="item br locEmail"><a href="mailto:'+self.email+'">Email</a></span>'); 
     } 

     // Add in the lat/long 
     infocontent.push('</span>'); 

     infocontent.push('<span class="item br locPosition"><strong>Lat:</strong> '+self.lat+'<br/><strong>Lng:</strong> '+self.lng+'</span>'); 

     // Create the infowindow for placement on the map, when a marker is clicked 
     self.infowindow = new google.maps.InfoWindow({ 
      content: infocontent.join(""), 
      position: self.point, 
      pixelOffset: new google.maps.Size(0, -15) // Offset the infowindow by 15px to the top 
     }); 

    }, 

    // Append the marker to the map 
    addToMap: function() { 
     var self = this; 

     self.marker.setMap(self.map); 
    }, 

    // Creates a sidebar module for the item, connected to the marker, etc.. 
    sidebarItem: function() { 
     var self = this; 

     if (self.sidebar) { 
      return self.sidebar; 
     } 

     var li = $('<li/>').attr({ 'class': 'location', 'id': 'location-'+self.id }), 
      name = $('<span/>').attr('class', 'locationName').html(self.name).appendTo(li), 
      address = $('<span/>').attr('class', 'locationAddress').html(self.address+' <br/> '+self.zipcode+' '+self.city+' '+self.country).appendTo(li); 

     li.addClass(self.rating_class); 

     li.bind('click', function(event) { 
      self.target(); 
     }); 

     self.sidebar = li; 

     return li; 
    }, 

    // This will "target" the store. Center the map and zoom on it, as well as 
    target: function(type) { 
     var self = this; 

     if (locator.targeted) { 
      locator.targeted.infowindow.close(); 
     } 

     locator.targeted = this; 

     if (type != 'map') { 
      self.map.panTo(self.point); 
      self.map.setZoom(14); 
     }; 

     // Open the infowinfow 
     self.infowindow.open(self.map); 
    } 
}; 

for (var i=0; i < locations.length; i++) { 
    var location = new Location(locations[i], self.map); 
    self.locations.push(location); 

    // Add the sidebar item 
    self.location_ul.append(location.sidebarItem()); 

    // Add the map! 
    location.addToMap(); 
}; 
+0

जो यहां http://www.geotweetmap.com/json_test पाया जा सकता है।php – Nick

-2

मार्कर पहले से ही विशिष्ट आईडी

marker.__gm_id 
+0

एकमात्र समाधान मैं इनमें से उपयोग करता हूं। – clime

+0

मेरे लिए काम नहीं करता है। अपरिभाषित आता है – John

7

बस एक और समाधान है कि मेरे लिए काम करता जोड़ने .. आप बस मार्कर विकल्पों में संलग्न कर सकते हैं और उसके बाद उन्हें पुनर्प्राप्त करें:

marker.get('store_id'); 
marker.get('store_address'); 
marker.get('store_type'); 
संबंधित मुद्दे

 संबंधित मुद्दे