2014-02-13 19 views
0

我正在使用公司列表模板,並希望顯示公司在Google地圖中的位置。這是我第一次使用Google Maps API和我的代碼工作。但我知道我目前沒有按照正確的方式去做。谷歌地圖的地理編碼工作,但不是正確的方式

<script> 

    var geocoder; 
    var map; 
    function initialize() { 
     geocoder = new google.maps.Geocoder(); 
     var address = '<?php Here I give the address from my database by PHP ?>'; 
     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 
      }); 
     } 
     }); 

     var latlng = new google.maps.LatLng(-34.397, 150.644); 
     var mapOptions = { 
     zoom: 16, 
     center: latlng 
     } 
     map = new google.maps.Map(document.getElementById('big_map'), mapOptions); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 

</script> 

<div id="big_map"></div> 

我想指定mapOptionscenterresults[0].geometry.location,但我不能得到它的工作。

正如我所說,我的代碼有效,但也許有人可以告訴我如何以更好的方式做到這一點,因爲我知道這不是100%正確的。

謝謝!

+0

您當前的代碼是你能做的最好的,這是100%正確的。當您想要直接在地圖選項中使用地理編碼器返回的位置時,您必須在地理編碼回調中創建地圖實例。但結果將是相同的,除非地圖渲染完成需要更多時間。 –

+0

好的,但那是我的嘗試。在地理編碼回調中創建地圖實例。但是如果你說渲染速度比較慢的話。然後我保持原樣。現在唯一不正確的是,我有一個來自澳大利亞的默認latlng。所以如果找不到地址,他會顯示澳大利亞地圖。或者我錯了? – Robbert

+1

這是正確的,但你必須指定一個地圖中心,這是一個必需的屬性。當地理編碼失敗時,您必須改用fallback-location(但結果與我所說的相同)。當你想有另一個默認中心時,定義另一個默認中心。還可以使用地理定位來檢索用戶的地址並將其用作後備。但問題是一樣的,地理定位可能會失敗。 –

回答

0

你有沒有試圖改變以獲得 「地圖」,像這樣...
第一:

var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var mapOptions = { 
    zoom: 16, 
    center: latlng 
    } 
map = new google.maps.Map(document.getElementById('big_map'), mapOptions); 

二:

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 
     }); 
    } 
}); 
+0

是的,但後來代碼不再工作。 – Robbert

+0

我想如果你用** mapOptions **來定位點,你可以刪除** map.setCenter(results [0] .geometry.location); **和** position:results [0] .geometry.location * *。 並試用「我的訂單」:) – Andynedine

+0

沒有,因爲我需要地理編碼。所以地址。 – Robbert

0

我用這個代碼,並加工:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 

<script> 
    var geocoder; 
    var map; 
    function initialize() { 
     geocoder = new google.maps.Geocoder(); 

     var latlng = new google.maps.LatLng(-34.397, 150.644); 
     var mapOptions = { 
      zoom: 16, 
      center: latlng 
     } 
     map = new google.maps.Map(document.getElementById('big_map'), mapOptions); 

     //var address = '<?php Here I give the address from my database by PHP ?>'; 
     geocoder.geocode({ 'address': 'Somewhere'}, 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 
      }); 
     }else { 
      alert('Geocode was not successful for the following reason: ' + status); 
     } 
    }); 

} 
google.maps.event.addDomListener(window, 'load', initialize); 

</script> 
<div id="big_map" class="googleMapsMunicipios"></div> 

欲瞭解更多信息,請訪問:

+0

謝謝你的回答! – Robbert