Javascript 未捕获类型错误:无法读取属性';纬度';未定义的

Javascript 未捕获类型错误:无法读取属性';纬度';未定义的,javascript,jquery,google-maps,google-maps-api-3,Javascript,Jquery,Google Maps,Google Maps Api 3,我正在尝试使用google maps javascript API创建一个网站,以查找城市周围的酒店,并将每个酒店与用户输入的位置之间的距离进行比较,以便找到离他们想要访问的所有位置最近的酒店 首先,它计算用户输入的位置的坐标(使用工作正常的地理编码API),然后获取酒店的坐标/google places信息 使用google.maps.geometry.Spheremic.ComputedDistanceBeween(LatlingA,LatlingB);要获取每个酒店与用户输入地址之间的距离

我正在尝试使用google maps javascript API创建一个网站,以查找城市周围的酒店,并将每个酒店与用户输入的位置之间的距离进行比较,以便找到离他们想要访问的所有位置最近的酒店

首先,它计算用户输入的位置的坐标(使用工作正常的地理编码API),然后获取酒店的坐标/google places信息

使用google.maps.geometry.Spheremic.ComputedDistanceBeween(LatlingA,LatlingB);要获取每个酒店与用户输入地址之间的距离,函数将返回以下错误:

未捕获类型错误:无法读取未定义的属性“lat”

但是,我传递给函数的两个LatLng参数似乎都不是null或未定义的

每个if(location1!=“”)、if(location2!=“”)等代码块都会检查酒店或位置的LatLng对象是否为null,如果为null,则会打印一个错误,但当我运行网页时,这些错误不会在javascript控制台中打印出来

下面是有问题的Javascript和HTML:


var映射;
var信息窗口;
函数initMap(){
var pyrmont={lat:-33.867,液化天然气:151.195};
map=new google.maps.map(document.getElementById('map'){
中心:皮尔蒙特,
缩放:15
});
infowindow=new google.maps.infowindow();
}
函数createMarker(place){
var placeLoc=place.geometry.location;
var marker=new google.maps.marker({
地图:地图,
位置:place.geometry.location
});
google.maps.event.addListener(标记'click',函数(){
infowindow.setContent(place.name);
打开(地图,这个);
});
}
var nearbyHotels=[];
var城市协调;
var城市协调平台;
功能findHotels()
{
var nearbyHotelsLat=[];
var nearbyHotelsLong=[];
var locationScores1=[];
var locationScores2=[];
var locationScores3=[];
var locationScores4=[];
var locationScores5=[];
var avgScores=[];
var geocoder=new google.maps.geocoder();
var cityAddress=document.getElementById('locations\u cityField')。值;
var radiusearch=parseInt(document.getElementById('locations\u rangeField').value);
var location1=document.getElementById('locations\u location1')。值;
var location2=document.getElementById('locations\u location2')。值;
var location3=document.getElementById('locations\u location3')。值;
var location4=document.getElementById('locations\u location4')。值;
var location5=document.getElementById('locations\u location5')。值;
var Location1Latling;
var location2LatLng;
var位置3LATLNG;
var位置4LATLNG;
var定位5LATLNG;
如果(位置1!=“”&位置1!=未定义)
location1LatLng=地理编码地址(地理编码器,位置1);
如果(位置2!=“”&位置2!=未定义)
location2LatLng=地理编码地址(地理编码器,位置2);
如果(位置3!=“”&位置3!=未定义)
location3LatLng=地理编码地址(地理编码器,位置3);
如果(位置4!=“”&位置4!=未定义)
location4LatLng=地理编码地址(地理编码器,位置4);
如果(位置5!=“”&位置5!=未定义)
location5LatLng=地理编码地址(地理编码器,位置5);
控制台日志(位置1标签);
var cityLoc=地理编码地址(地理编码者,城市地址);
setTimeout(函数(){
var coordSplit=城市协调。分割(“,”);
var-lati=parseFloat(coordSplit[0]);
var lngi=parseFloat(coordSplit[1]);
var city={lat:lati,lng:lngi};
var service=newgoogle.maps.places.PlacesService(地图);
服务,近距离搜索({
地点:北京市,
半径:半径搜索,
类型:[“住宿”]
},回调);
赛特中心地图(cityLoc);
setTimeout(函数(){
console.log(“nearbyHotels.length:+nearbyHotels.length”);
如果(位置1!=“”)
{
对于(变量i=0;ifunction geocodeAddress(geocoder, address) {
  console.log("address " + address);
  console.log("geocoder " + geocoder);
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      var coords = results[0].geometry.location.toString();
      console.log("CoordsRaw: " + coords)
      var coordsNoFirst = coords.substring(1);
      var coordsNoLast = coordsNoFirst.slice(0, -1);
      cityCoordinates = coordsNoLast;
      console.log(cityCoordinates);

      //*********** this will not do anything useful **********
      return results[0].geometry.location;
      //*******************************************************

    } else {
      alert('Could not convert an address to latitude/longitude for the google maps API: ' + status);
    }
  });
}