Google Maps JS Api-b.get不是函数错误(isLocationOnEdge) [英] Google Maps JS Api - b.get is not a function error(isLocationOnEdge)

查看:109
本文介绍了Google Maps JS Api-b.get不是函数错误(isLocationOnEdge)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的路线上是否有一个标记.因此,我尝试使用isLocationOnEdge(),并且出现"TypeError:b.get is not a function"错误. 这是我的代码,我尝试了几次更改,但无法解决问题.

I want to check if there is a marker on my route.. So i tried to use isLocationOnEdge() and i am getting "TypeError:b.get is not a function" error. Here is my code i tried several changes but couldn't fix the problem.

      var directionsDisplay = new google.maps.DirectionsRenderer;
      var directionsService = new google.maps.DirectionsService;

      directionsDisplay.setMap(map);

      calculateAndDisplayRoute(directionsService, directionsDisplay);

      function calculateAndDisplayRoute(directionsService, directionsDisplay) {
      directionsService.route({
      origin: my_position.getPosition(),
      destination: new google.maps.LatLng(my_markers[0][0],my_markers[0][1]),
      travelMode: 'DRIVING'
    }, function(response, status) {
      if (status === 'OK') {
        directionsDisplay.setDirections(response);
      } else {
        window.alert('Directions request failed due to ' + status);
      }
      var isLocationOnEdge = google.maps.geometry.poly.isLocationOnEdge;
      var path = response.routes[0].overview_path;
      for (var i = 0; i < my_markers.length; i++) {
      if (isLocationOnEdge(new google.maps.LatLng(my_markers[i][0],my_markers[i][1]),path))
          {
              console.log("Its in!")
          }
        }


    });

推荐答案

根据文档isLocationOnEdge方法采用以下参数:

According to the documentation, teh isLocationOnEdge method takes the following arguments:

isLocationOnEdge(point:LatLng,poly:Polygon | Polyline,tolerance?:number)
返回值:boolean
计算给定点是在指定公差范围内是位于折线上还是折线附近或多边形的边缘上.当所提供点的经纬度与边缘上最近的点之间的纬度和经度之差小于公差时,返回true.公差默认为10-9度.

isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number)
Return Value: boolean
Computes whether the given point lies on or near to a polyline, or the edge of a polygon, within a specified tolerance. Returns true when the difference between the latitude and longitude of the supplied point, and the closest point on the edge, is less than the tolerance. The tolerance defaults to 10-9 degrees.

overview_path不是google.maps.Polyline(或多边形),它是google.maps.LatLng对象的数组.

The overview_path is not a google.maps.Polyline (or a Polygon), it is an array of google.maps.LatLng objects.

如果我使用该路径创建折线,那么它对我有用

If I use that path to create a polyline, it works for me

var isLocationOnEdge = google.maps.geometry.poly.isLocationOnEdge;
var path = response.routes[0].overview_path;
var polyline = new google.maps.Polyline({
  path: path
})
if (isLocationOnEdge(new google.maps.LatLng(37.438442, -122.157558), polyline, 1e-3)) {
  console.log("Its in!")
}

概念提琴证明

代码段:

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      styles: [{
        featureType: 'poi',
        stylers: [{
          visibility: 'off'
        }]
      }]
    });

  var directionsDisplay = new google.maps.DirectionsRenderer;
  var directionsService = new google.maps.DirectionsService;

  directionsDisplay.setMap(map);

  calculateAndDisplayRoute(directionsService, directionsDisplay);

  function calculateAndDisplayRoute(directionsService, directionsDisplay) {
    directionsService.route({
      origin: "Palo Alto, CA",
      destination: "Stanford, CA",
      travelMode: 'DRIVING'
    }, function(response, status) {
      if (status === 'OK') {
        directionsDisplay.setDirections(response);
      } else {
        window.alert('Directions request failed due to ' + status);
      }
      var isLocationOnEdge = google.maps.geometry.poly.isLocationOnEdge;
      var path = response.routes[0].overview_path;
      var polyline = new google.maps.Polyline({
        path: path
      })
      if (isLocationOnEdge(new google.maps.LatLng(37.438442, -122.157558), polyline, 1e-3)) {
        console.log("Its in!")
        var mark = new google.maps.Marker({
          map: map,
          position: new google.maps.LatLng(37.438442, -122.157558),
          icon: {
            url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
            size: new google.maps.Size(7, 7),
            anchor: new google.maps.Point(3.5, 3.5)
          },
          title: "On Route"
        });
        var infowindow = new google.maps.InfoWindow({
          content: "on route"
        });
        infowindow.open(map, mark);
      }
    });
  }
}
google.maps.event.addDomListener(window, "load", initialize);

html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

这篇关于Google Maps JS Api-b.get不是函数错误(isLocationOnEdge)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆