2017-12-17 4 views
-1

내 경로에 마커가 있는지 확인하고 싶습니다. 그래서 isLocationOnEdge()를 사용하려고했는데 "TypeError : b. get은 함수가 아닙니다. "오류가 발생합니다. 여기에 내 코드가 몇 가지 변경 시도했지만 문제를 해결할 수 없습니다. the documentation 따르면Google지도 JS Api - b.get은 함수 오류가 아닙니다 (isLocationOnEdge)

 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!") 
      } 
     } 


    }); 

답변

0

, TEH 방법은 다음 인수를 사용

overview_path

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.

google.maps.Polyline없는 (또는 다각형)는 google.maps.LatLng 객체들의 어레이이다.

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"></script> 
 
<div id="map_canvas"></div>
: 나는 폴리 라인을 생성하는 경로를 사용하는 경우

, 그것은 멜

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!") 
} 

proof of concept fiddle

screenshot of resulting map

코드 작동

+0

고마워요! 당신은 진정한 영웅입니다! – PBSDR

관련 문제