2016-10-02 55 views
0

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

在我的HTML文件中,我调用了google map 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. 
} 

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

回答

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>

一后 “}”