Javascript 容器位置不工作

Javascript 容器位置不工作,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我尝试使用“google.maps.geometry.poly.containsLocation”检索布尔结果,问题是没有返回任何内容,我不知道是否需要在eventListener中调用它,但当我尝试调用它时,它开始说“pos”未定义,然后当我初始化pos时,它说“lng”未定义,我的代码有问题,或者我做的不对 JS: 地理位置服务是异步的,您需要在回调函数中使用位置,该位置在何处/何时可用 // Try HTML5 geolocation. if (navigator.geolocat

我尝试使用“google.maps.geometry.poly.containsLocation”检索布尔结果,问题是没有返回任何内容,我不知道是否需要在eventListener中调用它,但当我尝试调用它时,它开始说“pos”未定义,然后当我初始化pos时,它说“lng”未定义,我的代码有问题,或者我做的不对

JS:


地理位置服务是异步的,您需要在回调函数中使用位置,该位置在何处/何时可用

  // Try HTML5 geolocation.
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };


      infoWindow.setPosition(pos);
      infoWindow.setContent('You are Here.');
      map.setCenter(pos);
      // the containsLocation method requires a google.maps.LatLng as the first arg
      var isWithinPolygon = google.maps.geometry.poly.containsLocation(new google.maps.LatLng(pos.lat, pos.lng), zone);
      console.log(isWithinPolygon);
   // ...
代码的另一个问题是` containsLocation方法需要一个google.maps.LatLng作为“点”,在这里不能使用google.maps.LatLngLiteral

containsLocation(点:LatLng,多边形:多边形)|返回值:布尔值

计算给定点是否位于指定多边形内

代码片段:

var映射;
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
中心:{
纬度:41.185083,
液化天然气:-8.698814
},
缩放:12,
mapTypeId:google.maps.mapTypeId.TERRAIN
});
var infoWindow=new google.maps.infoWindow({
地图:地图
});
//试试HTML5地理定位。
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
var pos={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
信息窗口。设置位置(pos);
infoWindow.setContent('您在这里');
地图设置中心(pos);
//ContainesLocation方法需要一个google.maps.LatLng作为第一个参数
var isWithinPolygon=google.maps.geometry.poly.containsLocation(新的google.maps.LatLng(pos.lat,pos.lng),zone);
console.log(isWithinPolygon);
},函数(){
handleLocationError(true,infoWindow,map.getCenter());
});
}否则{
//浏览器不支持地理位置
handleLocationError(false,infoWindow,map.getCenter());
}
//定义分区的板条坐标。
可变区坐标=[{
拉脱维亚:41.194975,
液化天然气:-8.683102
}, {
拉脱维亚:41.183693,
液化天然气:-8.703740
}, {
纬度:41.205572,
液化天然气:-8.717563
}, {
纬度:41.203185,
液化天然气:-8.690135
}, {
拉脱维亚:41.194975,
液化天然气:-8.683102
}];
//建造这个区域。
var zone=new google.maps.Polygon({
路径:区域坐标,
strokeColor:“#023bca”,
笔划不透明度:0.8,
冲程重量:2,
填充颜色:“#0748eb”,
不透明度:0.35
});
区域。设置地图(地图);
}
功能手柄位置错误(浏览器具有地理位置、信息窗口、pos){
信息窗口。设置位置(pos);
infoWindow.setContent(browserHasGeolocation?
“错误:地理位置服务失败。”:
'错误:您的浏览器不支持地理位置。');
}
google.maps.event.addDomListener(窗口“加载”,initMap)
html,
身体,
#地图{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}

  // Try HTML5 geolocation.
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };


      infoWindow.setPosition(pos);
      infoWindow.setContent('You are Here.');
      map.setCenter(pos);
      // the containsLocation method requires a google.maps.LatLng as the first arg
      var isWithinPolygon = google.maps.geometry.poly.containsLocation(new google.maps.LatLng(pos.lat, pos.lng), zone);
      console.log(isWithinPolygon);
   // ...
// the containsLocation method requires a google.maps.LatLng as the first arg
var isWithinPolygon = google.maps.geometry.poly.containsLocation(new google.maps.LatLng(pos.lat, pos.lng), zone);
console.log(isWithinPolygon);