2013-05-30 24 views
0

我下面這個tuorial - https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple谷歌的地理編碼,而不是通過輸入

我已經把代碼<?php echo the_field('post_code'); ?>,但我想它地理編碼的負載,而不是通過輸入並提交。我怎樣才能做到這一點?

這是我有:

<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(52.375599, -3.471680); 
    var mapOptions = { 
    zoom: 8, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
} 

function codeAddress() { 
    var address = '<?php echo the_field('post_code'); ?>'; 
    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 
     }); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    }); 
} 

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

    </script> 
<div id="map-canvas" style="float:left;height:250px; width:625px;margin-top:25px;"></div> 
+0

嗨,羅布我沒有得到你。你想要沒有提供輸入地址或提交按鈕的地理編碼?和'<?php回顯the_field('post_code'); ?>「這有什麼問題? –

+0

@LakshmanaKumar是的,我想要加載地理編碼。在教程中,您必須提供一個地址並點擊提交。我通過'<?php echo the_field('post_code')設置地址; '',所以我想讓它立即對其進行地理編碼並放置標記。 – Rob

+0

地理編碼和地圖初始化後調用'codeAddress'。 –

回答

1
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(52.375599, -3.471680); 
    var mapOptions = { 
    zoom: 8, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 

    codeAddress(); // This should do it. Assuming all the code is working. 
} 
+0

嗯,試圖的時候地圖不會加載。我保留下面的代碼,只是添加了你建議的代碼。這是我正在處理的頁面 - http://www.mediwales.com/pictureofhealth/teamworks-design-and-marketing/ – Rob

+0

您在控制檯中遇到的任何錯誤? –

+0

明白了,我的愚蠢的錯誤,你的答案奏效,謝謝。 – Rob

0

呼叫codeAddress()initialize()通過地址作爲參數。

function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(52.375599, -3.471680); 
    var mapOptions = { 
    zoom: 8, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    codeAddress(address); 
}