2012-01-06 78 views
2

我试图当前显示页面加载时的地址的位置,但我的地图根本不显示。我试图提醒(地址),它的工作原理。它显示了我想要得到的正确地址。但我不确定我是否做得对?显示地址位置与谷歌地图onload

<script> 
    var geocoder; 
    var map; 
    function codeAddress() { 
    geocoder = new google.maps.Geocoder(); 
    //var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var myOptions = { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
      var city_state_zip = document.getElementById("city_state_zip").innerHTML; 
      var street_address = document.getElementById("street_address").innerHTML; 
      var address = street_address + " " + city_state_zip;//document.getElementById(street_addres +" "+ city_state_zip).value; 
      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); 
       } 
      }); 
      } 



    window.onload = function(){ 
      codeAddress(); 
     } 
</script> 

我结合初始化()与codeAddress()在这个例子中发现:http://code.google.com/apis/maps/documentation/javascript/geocoding.html#GeocodingStatusCodes我想,因为我不希望加载与随机经纬度地图结合这一点,我希望它加载的原因我已经有了一个地址。

有没有人有任何想法,如果我甚至正确地做到这一点?

谢谢!

+0

既然你有latlng注释掉我不知道这是否会导致问题。此外,你是否能够在没有所有地理编码的情况下显示地图? – asawilliams 2012-01-06 04:58:03

+0

当我取消对latlng的注释时,它会转到特定的latlng,然后直接转到所需的地址,我如何不让它转到latlng并直接寻址onload? – hellomello 2012-01-06 05:20:08

+1

在设置地图选项并使用结果[0] .geometry.location填充选项的中心属性之前,请先执行所有地理编码器的工作内容 – asawilliams 2012-01-06 05:39:29

回答

4

脚本的下列修改使其适用于与我处在同一条船上的人员。这现在允许我加载页面时加载一个特定的地址,它会自动检测我的html中的lat,lng。

这是从谷歌的网站稍作修改。

<script> 
    var geocoder; 
    var map; 
    function codeAddress() { 
    geocoder = new google.maps.Geocoder(); 
    var lat=''; 
    var lng='' 
    var city_state_zip = document.getElementById("city_state_zip").innerHTML; 
    var street_address = document.getElementById("street_address").innerHTML; 
    var address = street_address + " " + city_state_zip; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     lat = results[0].geometry.location.lat(); //getting the lat 
     lng = results[0].geometry.location.lng(); //getting the lng 
     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); 
      } 
    }); 
    var latlng = new google.maps.LatLng(lat, lng); 
    var myOptions = { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    } 


window.onload = function(){ 
     codeAddress(); 
} 
</script> 
+0

然后,您应该使用工作代码编辑此答案并接受您自己的答案!这比在一起的问题和答案要好 – 2012-01-06 06:30:38

+0

我会把工作代码放在这里,但直到几天后我才接受它 – hellomello 2012-01-06 16:10:15