【问题标题】:Google Maps API: Geocoder: unknown property function:Google Maps API:地理编码器:未知属性功能:
【发布时间】:2016-10-02 06:09:49
【问题描述】:

我目前正在尝试使用 Google Geocoder API 将地址转换为 Lat/Lng 坐标,但是,在使用他们的开发文档 (https://developers.google.com/maps/documentation/javascript/geocoding) 上的示例时,我遇到了“InvalidValueError:未知属性”错误函数”

在我的 HTML 文件中,我调用了谷歌地图 API 和地图 div,我的地图应该在其中呈现。 (当然我也有其他的东西,但为了直截了当,我只会贴出相关代码)

<script src="https://maps.googleapis.com/maps/api/js?key=APIKEY&callback=initMap" async defer></script>
<div id="map"></div>

在我的 JS 文件中:

window.initMap = function(){
var fromLocation = new google.maps.LatLng(37.09024, -95.712891),
  destLocation = new google.maps.LatLng(37.09024, -95.712891),
  map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: 37.09024 , lng: -95.712891}, // center of USA
    zoom: 4 // continet level
  }),
  directionService = new google.maps.DirectionsService(),
  directionRender = new google.maps.DirectionsRenderer({
    map: map
  }),
  markerA = new google.maps.Marker({
    position: fromLocation,
    title: "Point A",
    label: "From",
    map:map
  }),
  markerB = new google.maps.Marker({
    position: destLocation,
    title: "Point B",
    label:"Destination",
    map:map
  });
   getLatLng("Los Angeles");
 } // end of initMap

 function getLatLng(address){
   geocoder = new google.maps.Geocoder();
   geocoder.geocode({address: address, function(results,status){
    if(status === 'OK'){
      console.log(results[0].geometry.location);
    }
    else{
      console.log("Geocoding couldn't convert string address to lat/long points");
    }
  }
 });// end of geocoder method.
}

关于如何修复未知属性函数错误的任何想法? 谢谢!

【问题讨论】:

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


    【解决方案1】:

    你的函数getLatLng有错字:

    geocoder.geocode({
      address: address,
    

    address: address之后需要有一个“}”

    function getLatLng(address) {
      geocoder = new google.maps.Geocoder();
      geocoder.geocode({
        address: address},
        function(results, status) {
          if (status === 'OK') {
            console.log(results[0].geometry.location);
          } else {
            console.log("Geocoding couldn't convert string address to lat/long points");
          }
      }); // end of geocoder method.
    }
    

    window.initMap = function() {
        var fromLocation = new google.maps.LatLng(37.09024, -95.712891),
          destLocation = new google.maps.LatLng(37.09024, -95.712891),
          map = new google.maps.Map(document.getElementById('map'), {
            center: {
              lat: 37.09024,
              lng: -95.712891
            }, // center of USA
            zoom: 4 // continet level
          }),
          directionService = new google.maps.DirectionsService(),
          directionRender = new google.maps.DirectionsRenderer({
            map: map
          }),
          markerA = new google.maps.Marker({
            position: fromLocation,
            title: "Point A",
            label: "From",
            map: map
          }),
          markerB = new google.maps.Marker({
            position: destLocation,
            title: "Point B",
            label: "Destination",
            map: map
          });
        getLatLng("Los Angeles");
      } // end of initMap
    
    function getLatLng(address) {
      geocoder = new google.maps.Geocoder();
      geocoder.geocode({
          address: address
        },
        function(results, status) {
          if (status === 'OK') {
            console.log(results[0].geometry.location);
          } else {
            console.log("Geocoding couldn't convert string address to lat/long points");
          }
        }); // end of geocoder method.
    }
    html,
    body,
    #map {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>
    <div id="map"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2010-11-21
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 2013-10-05
      相关资源
      最近更新 更多