2014-03-02 159 views
0

我有兩個腳本。一個州/縣下拉的時候會激活,它會進行地址查詢並轉到該特定的縣。我已經在窗體中填充了「經度」和「緯度」文本框。我正試圖將兩者結合起來。後者腳本使用靜態經度/緯度,我需要它使用像我在第一個腳本中使用的地址。在試圖將這兩者結合起來的時候,我在我的代碼中有一處錯誤。地圖甚至不會出現。谷歌地圖地址沒有顯示

function selectState(state_id){ 
    if(state_id!="-1"){ 
     loadData('city',state_id); 
var e = document.getElementById("state"); 
var stateloc = e.options[e.selectedIndex].value; 

    var sAddress = state_id + stateloc; 

    var $latitude = document.getElementById('latitude'); 
    var $longitude = document.getElementById('longitude'); 
    //var latitude = 33.00636021320537 
    // var longitude = -93.46687316894531; 
    var zoom = 12; 
    var geocoder; 

    //var LatLng = new google.maps.LatLng(latitude, longitude); 
geocoder = new google.maps.Geocoder(); 

geocoder.geocode({ 'address': sAddress}, function(results, status) 
{ 
    if (status == google.maps.GeocoderStatus.OK) 
    { 


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



var mapOptions = { 
    zoom: zoom, 
    //center: LatLng, 
    panControl: false, 
    zoomControl: true, 
    scaleControl: true, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
} 

var map = new google.maps.Map(document.getElementById('map'),mapOptions); 

var marker = new google.maps.Marker({ 
// position: LatLng, 
position: results[0].geometry.location, 
    map: map, 
    title: 'Drag Me!', 
    draggable: true 
}); 

google.maps.event.addListener(marker, 'dragend', function(marker){ 
    var latLng = marker.latLng; 
    $latitude.value = latLng.lat(); 
    $longitude.value = latLng.lng(); 
}); 




//end geocoder check 
     } 
    else 
    { 
      alert("Geocode was not successful: " + status); 
    } 

    }); 




    }else{ 
     $("#city_dropdown").html("<option value='-1'>Select city</option>"); 
    } 
    } 

HTML

<li> 
    <label for="lat">Latitude:</label> 
    <input id="latitude" placeholder="latitude" name="lat" type="text" class="text" /> 
</li> 
<li> 
    <label for="lon">Longitude:</label> 
    <input id="longitude" placeholder="longitude" name="lon" class="text" type="text"/> 
</li> 

    <div id="map" style="width: 460px; height: 350px;"></div> 
+0

在你的HTML沒有地圖。 – geocodezip

回答

0

您可以設置不確定的地圖中心。所以,如果其他一切正常,你必須改變這部分:

 ... 
     if (status == google.maps.GeocoderStatus.OK) { 
      // map is undefined here 
      // map.setCenter(results[0].geometry.location); 

      var mapOptions = { 
       zoom: zoom, 
       // center could be set here 
       center: results[0].geometry.location, 
       panControl: false, 
       zoomControl: true, 
       scaleControl: true, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      } 

      // map is created here 
      var map = new google.maps.Map(document.getElementById('map'),mapOptions); 
      ... 
+0

我註釋掉了map.setCenter(results [0] .geometry.location);現在我得到一個空白的灰色方塊來代替地圖顯示的地方。我是否在代碼中的錯誤位置定義了地圖? –

+0

我的方向是否正確?我需要做什麼?我應該定義一張地圖還是我已經定義了一張地圖?謝謝。 –

+0

如果尚未設置居中和縮放,則地圖將變爲「灰色」(未初始化)。您可以在創建地圖時(在用於創建地圖的mapOptions中)或者稍後(在創建之後)使用setCenter和setZoom來做到這一點。 – geocodezip