2010-11-06 8 views
6

में Google मानचित्र जियोकोडिंग और मार्कर मैं पूरी तरह से परेशान हूं। मेरे पास प्रत्येक स्थान वाले ऑब्जेक्ट्स की एक सूची है। मैं google.maps.geocoder का उपयोग करके इस स्थान को देखता हूं और बाद में मैंने मानचित्र पर उस स्थान के लिए एक मार्कर लगाया।लूप

लेकिन किसी कारण से केवल एक मार्कर दिखाई देता है। मुझे लगता है कि इसे बंद करने की समस्या के साथ करना है जो मैंने यहां के अन्य धागे में देखा है, लेकिन मैं बस मेरे पास जो भी है उसके समाधान को लागू नहीं कर सकता।

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    map.fitBounds(bounds); 

    for (var item in list) { 
    var geocoder = new google.maps.Geocoder(); 
    var geoOptions = { 
     address: item.location, 
     bounds: bounds, 
     region: "NO" 
    }; 
    geocoder.geocode(geoOptions, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     addMarker(map, item, results[0].geometry.location); 
     } else { 
     console.log("Geocode failed " + status); 
     } 
    }); 
    } 

function addMarker(map, item, location) { 
    var marker = new google.maps.Marker({ map : map, position : location}); 
    marker.setTitle(item.title); 
    var infowindow = new google.maps.InfoWindow({ 
    content : item.body, 
    size : new google.maps.Size(100, 300) 
    }); 
    (function(map, marker) { 
    new google.maps.event.addListener(marker, "click", function() { 
     infowindow.open(map, marker); 
    }); 
    })(map, marker); 
    } 

किसी भी मदद की सराहना की है:

मेरे कोड इस प्रकार है।

अद्यतन: पहले उत्तर में सुझाव के रूप में छोरों में बंद किया जाना, मैं इस के लिए कोड बदल दिया है से बचने के लिए:

//This is the entry 
function codeLocations(list, map) { 
    for (var i = 0; i < list.length; i++) { 
    console.log("Looping " + list[i].location); 
    var geocoder = new google.maps.Geocoder(); 
    var geoOptions = { 
     address: list[i].location, 
     bounds: getBounds(), 
     region: "NO" 
    }; 
    geocoder.geocode(geoOptions, createGeocodeCallback(list[i], map)); 
    } 
} 

function createGeocodeCallback(item, map) { 
    console.log("Generating geocode callback for " + item.location); 
    return function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     console.log("Geocoding " + item.location + " OK"); 
     addMarker(map, item, results[0].geometry.location); 
    } else { 
     console.log("Geocode failed " + status); 
    } 
    } 
} 

function addMarker(map, item, location) { 
    console.log("Setting marker for " + item.location + " (location: " + location + ")"); 
    var marker = new google.maps.Marker({ map : map, position : location}); 
    marker.setTitle(item.title); 
    var infowindow = new google.maps.InfoWindow({ 
    content : item.body, 
    size : new google.maps.Size(100, 300) 
    }); 
    new google.maps.event.addListener(marker, "click", function() { 
    infowindow.open(map, marker); 
    }); 
} 

लॉग बयान के अनुसार, अब मैं सही स्थानों पर सही ऑब्जेक्ट , जिसका मतलब है कि मार्कर सेट होने पर आइटम और स्थान ऑब्जेक्ट अलग-अलग होते हैं, लेकिन मुझे अभी भी केवल मेरे मानचित्र पर एक मार्कर मिलता है। यह कैसे हो सकता है?

उत्तर

8

लूप में बंद न करें। That just won't work। यह समस्या का समाधान हो सकता है:

function callback() { 
    return function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     addMarker(map, item, results[0].geometry.location); 
     } else { 
     console.log("Geocode failed " + status); 
     } 
    }; 
    } 

    for (var item in list) { 
    var geocoder = new google.maps.Geocoder(); 
    var geoOptions = { 
     address: item.location, 
     bounds: bounds, 
     region: "NO" 
    }; 
    geocoder.geocode(geoOptions, callback()); 
    } 
+0

इसके लिए धन्यवाद। मैंने उस से बचने के लिए कोड अपडेट किया है लेकिन यह समस्या को ठीक नहीं कर रहा है: -/ – fiskeben

+0

@ फिस्कबेन, क्या आप हमें एक ऑनलाइन उदाहरण दे सकते हैं, – Harmen

+0

डीबग करना बहुत आसान है, http: // riccomadsen आज़माएं। com/maptest.html – fiskeben