【问题标题】:Google Maps JavaScript API V3 - Geocoder API is not returning place nameGoogle Maps JavaScript API V3 - Geocoder API 不返回地名
【发布时间】:2017-02-25 07:05:46
【问题描述】:

这是我的代码,我是 Google 地图和 JavaScript 的新手,我试图通过将位置 (latlng) 传递给地理编码器来获取“地名”,但它没有返回任何值。如果将 placeName var 设置为“somePlace”,则将标题设置为“somePlace”。但我想用返回值设置标题,即地名。

function setMap(position){
  //var Karachi = {lat: 24.8978176, lng: 66.9596003}; //default city
  var location = {
    lat: position.coords.latitude,
    lng: position.coords.longitude
  }

  var mapOptions = {

      center: location,
      zoom: 13,         //initial zoom level
      mapTypeId: 'hybrid'  //map type
  }
  //object of map
  var map = new google.maps.Map(document.getElementById('map'), mapOptions);

  var placeName;
  //placeName = 'SomePlace'; 
  var newLatLng = location + '';
  var latlngStr = newLatLng.split(',',2);
  var latlng = {lat: parseFloat(latlngStr[0]), lng:    parseFloat(latlngStr[1])};
  var geocoder = new google.maps.Geocoder({'location': latlng}, function(results, status) {
    if(status === 'OK'){
      if(results[1]){
        placeName = 'results[1].formatted_address';
      }
      else{
        window.alert('Error in ' + status);
      }
    }
    else{
      window.alert('Geocoder failed due to ' + status);
    }
  });

  var markerOptions = {
    position: location,
    map: map,
    title: placeName //<-- i want place name to be here

  }
  //object of marker
  var marker = new google.maps.Marker(markerOptions);
  marker.setTitle(placeName); //<-- this is also not working
}

【问题讨论】:

    标签: javascript google-maps-api-3 google-maps-markers reverse-geocoding google-geocoding-api


    【解决方案1】:

    您的代码中有语法错误。

    1. 您需要使用有效的GeocoderRequest 对象调用google.maps.Geocoder geocode 方法:
    var geocoder = new google.maps.Geocoder();
      geocoder.geocode({
        location: location
      }, function(results, status) 
    
    1. Geocoder 是异步的,您需要在回调函数中使用返回的结果时/在可用的地方:
      if (results[1]) {
        placeName = results[1].formatted_address;
        var markerOptions = {
            position: location,
            map: map,
            title: placeName //<-- i want place name to be here
    
          }
          //object of marker
        var marker = new google.maps.Marker(markerOptions);
    

    proof of concept fiddle

    代码 sn-p:

    function setMap(position) {
      var location = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      }
    
      var mapOptions = {
    
        center: location,
        zoom: 13, //initial zoom level
        mapTypeId: 'hybrid' //map type
      }
      //object of map
      var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    
      var geocoder = new google.maps.Geocoder();
      geocoder.geocode({
        location: location
      }, function(results, status) {
        if (status === 'OK') {
          if (results[1]) {
            placeName = results[1].formatted_address;
            var markerOptions = {
              position: location,
              map: map,
              title: placeName // set formatted_address as title of marker
            }
            //object of marker
            var marker = new google.maps.Marker(markerOptions);
          } else {
            window.alert('Error in ' + status);
          }
        } else {
          window.alert('Geocoder failed due to ' + status);
        }
      });
    }
    google.maps.event.addDomListener(window, "load", function() {
      setMap({
        coords: {
          latitude: 37.4419,
          longitude: -122.1419
        }
      })
    });
    html,
    body,
    #map {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="map"></div>

    【讨论】:

      【解决方案2】:

      只需尝试将您的 lat 和 long 传递给以下查询字符串,您将获得一个 json 数组,从那里获取您的地名。

      http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true
      

      【讨论】:

      • 我试过了,但它返回“Zero_Results”,不知道现在该怎么办。
      • 你试过把这个网址粘贴到浏览器吗?
      • 我使用了您提供的链接,它工作正常,它给了我一个 json 对象,但是当我尝试传递地理定位的 latlng 时,它给出了 zero_results,我还尝试了浏览器传感器并将位置设置为伦敦和孟买,但没有得到任何结果。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2013-10-05
      相关资源
      最近更新 更多