2013-04-20 25 views
0

我需要使用MapsV3 API通過地址或緯度/經度來繪製多個自定義地圖標記。我把下面的代碼放在一起,它可以很好地處理lon/lat,但是如果數據包含一個地址,那麼geocoder什麼也不返回。有想法該怎麼解決這個嗎。谷歌地圖v3通過地址或latlng多個自定義標記地理編碼

$(document).ready(function() { initialize(); }); 

    function initialize() { 
     var centerMap = new google.maps.LatLng(-27.0,133.0); 

     var options = { 
      panControl: false, 
      zoomControl: true, 
      scaleControl: false, 
      mapTypeControl: false, 
      streetViewControl: false, 
      zoom: 3, 
      center: centerMap, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 

     var map = new google.maps.Map(document.getElementById("map"), options); 




        var data = [ 
        { 
        'title':'F C Building Construction ...', 
        'address':'', 
        'zindex':20, 
        'lat':'-33.797847', 
        'lon':'151.259928', 
        'marker_number':1, 
        'marker_html':' html content...', 
        'image': { 
         url:'//localhost/assets/images/markers/marker_1.png', 
         size: new google.maps.Size(24, 29), 
         origin: new google.maps.Point(0,0), 
         anchor: new google.maps.Point(10, 29) 
        } 
        }]; 

      setMarkers(map, data); 

        var infowindow = new google.maps.InfoWindow(); 

    } 

    function showMapPin(i) { } 

    function setMarkers(map,data) {   
     var bounds = new google.maps.LatLngBounds(); 
     for (var i = 0; i < data.length; i++) { 
      setMarker(map, data[i], bounds); 
     } 
     map.fitBounds(bounds); 
    } 

    function setMarker(map, m, bounds) { 

     if(m["address"]!="") { 
      var geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({ "address": m["address"] }, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        var siteLatLng = results[0].geometry.location; 
       } 
      }); 


     } else { 
      var siteLatLng = new google.maps.LatLng(m["lat"], m["lon"]); 
     } 

     if(siteLatLng) { 

      var marker = new google.maps.Marker({ 
       position: siteLatLng, 
       map: map, 
       //shadow: shadow, 
       icon: m["image"], 
       title: m["title"], 
       zIndex: m["zindex"], 
       html: m["marker_html"] 
      }); 

      bounds.extend(siteLatLng); 

      google.maps.event.addListener(marker, "click", function() { 
       infowindow.setContent(this.html); 
       infowindow.open(map, this); 
      }); 

     } // if latlng 
    }//]]> 

回答

0

回顧過去的代碼後,我意識到,可變siteLatLng是地理編碼的功能範圍內,所以它永遠不會被通過,就不能退換。在此功能中設置標記。