为什么我的反向地址解析无法正常工作并无法显示? [英] Why is my reverse geocoding not working and displaying?

查看:84
本文介绍了为什么我的反向地址解析无法正常工作并无法显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有附近的搜索地图,在此地图页面的每次打开中,它都会返回当前位置,现在当我通过坐标获取当前位置时,我想将其反向地理编码为地址名称,问题是我修改了我来自以下来源的代码: https: //developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/geocoding-reverse

I have a nearby search map, in every open of this map page, it returns the current position, now When I get the current position by coordinates, I want to reverse geocode it into an address name, the problem is I modified my code from this source: https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/geocoding-reverse

使用

<script>
function getPosition() {
  navigator.geolocation.getCurrentPosition(position => {
    currentLatLon = [position.coords.latitude, position.coords.longitude];
    infowindow = new google.maps.InfoWindow();
    map = new google.maps.Map(
      document.getElementById('map'), {
        center: new google.maps.LatLng(...currentLatLon),
        zoom: 20
      });
      var geocoder = new google.maps.Geocoder();       
    service = new google.maps.places.PlacesService(map);
    document.getElementById("curr").innerHTML=currentLatLon;
    document.getElementById("address").value=currentLatLon;
    geocodeLatLng(geocoder,map,infowindow);
  });
}

 function geocodeLatLng(geocoder, map, infowindow) {
        var input = document.getElementById('curr').value;
        var latlngStr = input.split(',');
        var latlng = {lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1])};
        geocoder.geocode({'location': latlng}, function(results, status) {
          if (status === 'OK') {
            if (results[0]) {
              map.setZoom(11);
              var marker = new google.maps.Marker({
                position: latlng,
                map: map
              });
              infowindow.setContent(results[0].formatted_address);
              infowindow.open(map, marker);
            } else {
              window.alert('No results found');
            }
          } else {
            window.alert('Geocoder failed due to: ' + status);
          }
        });
      }
    </script>

这应该返回地图中的地名,就像我从上面复制的源代码一样 https://developers-dot- devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/geocoding-reverse ,我的修改有什么问题?运行修改后的代码时,控制台出现错误,控制台出现错误

this should return the place name in the map which is like the source code I copied from above https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/geocoding-reverse, what could be wrong in my modification? I have an error in the console when I run my modified code, error in the console

这是我没有api键的完整代码: https://pastebin.com/BhEqRsq0

Here's my full code without the api key: https://pastebin.com/BhEqRsq0

推荐答案

您将lat/lng坐标设置为<p>元素的innerHTML,而不是其(不受支持的)value,这就是为什么它返回未定义的原因:

You set the lat/lng coordinates to the <p> element's innerHTML, not to its (unsupported) value which is why it returns undefined:

document.getElementById("curr").innerHTML = currentLatLon;

因此更改此代码:

var input = document.getElementById('curr').value;

到以下:

var input = document.getElementById('curr').innerHTML;

我刚刚在终端运行了您的Web应用,在完成上述修复后,反向地理编码可以正常工作.希望对您有所帮助!

I just ran your web app on my end and reverse geocoding works fine after the above fix. So hope this helps!

这篇关于为什么我的反向地址解析无法正常工作并无法显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆