简体   繁体   English

Google Maps API v3-地理编码

[英]Google Maps API v3 - Geocoding

i would like to get a map with 2 markers. 我想要一张带有2个标记的地图。 if i use the LatLng (see JS Code / WORKS JUST FINE /) it is no problem... but as soon i try to use geocoding (see JS Code / YU NOT WORKING? /) it doesn't work. 如果我使用LatLng(请参阅JS代码/做得很精细 /),这没问题...但是,一旦我尝试使用地理编码(请参见JS代码/无法工作 /),它就不起作用了。 I guess this is all wrong: 我想这都是错误的:

var adresse3=geocoder.geocode({'address': 'Winkelriedstrasse 47, 6004 Luzern'})[0].geometry.location; var adresse3 = geocoder.geocode({'address':'Winkelriedstrasse 47,6004 Luzern'})[0] .geometry.location;

Can anyone help me. 谁能帮我。 Thanks you in advance. 预先谢谢你。

var myCenter=new google.maps.LatLng(47.050944,8.309441);
var geocoder;
var map;
function initialize(){  
    var mapProp = {
      center            :myCenter         
    };
    var geocoder = new google.maps.Geocoder();
    var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
    /*WORKS JUST FINE*/
    var marker1=new google.maps.Marker({
        position: new google.maps.LatLng(47.050944,8.309441)
    });
    marker1.setMap(map);
    var infowindow1 = new google.maps.InfoWindow({
        content:"marker1"
    });
    google.maps.event.addListener(marker1, 'click', function() {
        infowindow1.open(map,marker1);
    });  
    /*Y U NOT WORKING?*/
    var adresse3=geocoder.geocode({'address': 'Winkelriedstrasse 47, 6004 Luzern'})[0].geometry.location;
    var marker3=new google.maps.Marker({
        position: adresse3
    });
    marker3.setMap(map);
    var infowindow3 = new google.maps.InfoWindow({
        content:"marker3"
    });
    google.maps.event.addListener(marker3, 'click', function() {
        infowindow3.open(map,marker3);
    }); 
}
google.maps.event.addDomListener(window, 'load', initialize);

Your problem is that geocoder.geocode is asynchronous, so you have to pass a callback function . 您的问题是geocoder.geocode是异步的,因此您必须传递一个回调函数

Adapted from the Google Maps Documentation : 改编自Google Maps文档

geocoder.geocode({'address': 'Winkelriedstrasse 47, 6004 Luzern'}, 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);
  }
});

thats how i solved it (i used the goMap PlugIn http://www.pittss.lv/jquery/gomap/ ) 多数民众赞成我解决它的方式(我使用了goMap插件http://www.pittss.lv/jquery/gomap/

<script type="text/javascript" src="js/jquery.gomap-1.3.2.min.js"></script> 
<script type="text/javascript">
$(document).ready(function() {
    $(function(){ 
        $("#googleMap").goMap({ 
            address: 'Luzern, Schweiz', 
            zoom: 13,
            maptype: 'ROADMAP',
            mapTypeControl: false,  
            hideByClick: false
        }); 
        $.goMap.createMarker({  
            address: 'Winkelriedstrasse 47, 6004 Luzern',
            html: 'Winkelriedstrasse 47, 6004 Luzern' 
        });
    });
});
</script>

now it works just fine... 现在工作正常...

Nicolas, you have an error in line: 尼古拉斯(Nicolas),您在行中出错:

geocoder.geocode('address': 'Winkelriedstrasse 47, 6004 Luzern', function(results, status) 

use this code: 使用此代码:

geocoder.geocode({'address': 'Winkelriedstrasse 47, 6004 Luzern'}, function(results, status) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM