【问题标题】:Google map to show name of location if found, otherwise just the address如果找到,谷歌地图显示位置名称,否则只显示地址
【发布时间】:2014-07-24 21:14:50
【问题描述】:

我是否可以在我的网站中使用嵌入的 Google 地图来显示找到的位置名称,如果找不到则只显示地址?

我可以让我的代码在所有情况下显示地址,但名称仅在某些情况下显示。我在http://www.yankeedesi.com/testmap.htm 中有几个例子。如果您查看第一行,我可以使用地址而不是地名来获取地图。然而,在第二行,这两种情况都有效。

我第一个使用的代码是:

<iframe width="300" height="250" frameborder="0" style="border:0"
 src="https://www.google.com/maps/embed/v1/place?key=<MyKey>&q=910+11th+St,Anacortes+WA+98221">    
</iframe>

第二个是

<iframe width="300" height="250" frameborder="0" style="border:0" 
src="https://www.google.com/maps/embed/v1/place?
key=AIzaSyCpmbSHS59rVuyJGtIStR4CcGo2g3dNXF8
&q=Little+Calcutta+Restaurants,910+11th+St,Anacortes+WA+98221">
</iframe>

现在在第二种情况下,因为谷歌地图无法找到名称为 Little+Calcutta+Restaurants 的地址,所以它也没有搜索地址。

我想要的是: 如果使用名称搜索成功,则显示名称,否则使用地址搜索并忽略名称。这可能吗?

【问题讨论】:

    标签: google-maps google-maps-api-3


    【解决方案1】:

    是的,使用地理编码器

    var geocoder;

    在制作地图之前将此添加到您的初始化中

    geocoder = new google.maps.Geocoder();

    function search(){
     var address = document.getElementById('address').value;
    

    geocoder.geocode( { 'address': address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { //search complete } else { //search again but subtring your address }

    【讨论】:

    • 感谢使用 Geocoder 的提示。我的代码在 Classic ASP 中,所以我在 Classic ASP 中查找了 Geocoder 的用法,并使用了stackoverflow.com/questions/22670925/… 中提供的解决方案。但是,我注意到在某些情况下,地址已经映射到其他位置名称,该名称会显示,而不是我传递的名称。所以,我决定在所有情况下都坚持显示地址而不是名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多