簡體   English   中英

Google Maps API:Geocoder:未知屬性函數:

[英]Google Maps API: Geocoder: unknown property function:

我目前正在嘗試使用Google Geocoder API將地址轉換為緯度/經度坐標,但是,在其開發人員文檔( https://developers.google.com/maps/documentation/javascript/geocoding )上使用示例時,我遇到錯誤“ InvalidValueError:未知屬性函數”

在我的HTML文件中,我調用了Google Map API和一個Map 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.
}

關於如何修復未知屬性函數錯誤的任何想法? 謝謝!

您在getLatLng函數中有一個錯字:

geocoder.geocode({
  address: address,

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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM