【问题标题】:Get directions to nearest marker maps javascript API获取到最近的标记地图 javascript API 的路线
【发布时间】:2021-05-29 09:35:27
【问题描述】:

我正在尝试获取离用户位置最近的标记,然后在单击按钮时获取方向,我已将标记传递给 getDirections(markers),但是当函数 google.maps.geometry.spherical.computeDistanceBetween 采用 google.maps.LatLng 对象时会抛出错误 @987654324 @

但是标记数组和 userLocation 都可以处理数据(不是未定义),所以我无法理解发生了什么。

代码:

jQuery(document).ready(function () {
  // Create the script tag, set the appropriate attributes
  
  let script = document.createElement('script');
  script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyBMCq6Fj4pN0Ku5ScVza28FZw0beM&callback=initMap&libraries=places&libraries=geometry';
  script.defer = true;
  var map
  let infowindow

  window.initMap = function () {
    const center = {
      lat: -36.561551,
      lng: -72.0954877
    }
    infowindow = new google.maps.InfoWindow();
    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 5,
      center: center
    });
    let markers = setMarkers(map)
    getDirections(markers)

  }
  const locations = [
    ['Super Pellet Santiago', -33.458717664930084, -70.77513497336462],
    ['Super Pellet Chillan', -36.561551, -72.0954877],
    ['Super Pellet Concepción', -36.8158124, -73.0741686],
    ['Super Pellet Los Angeles', -37.4774907, -72.3245759],
    ['Super Pellet Angol', -33.80010128657071, 151.28747820854187],
    ['Super Pellet Temuco', -38.7702088, -72.6301967]
  ];
  function setMarkers(map) {
    let markers = []
    for (let i = 0; i < locations.length; i++) {
      markers[i] = new google.maps.Marker({
        title: locations[i][0],
        position: {
          lat: locations[i][1],
          lng: locations[i][2]
        },
        map: map
      });
      google.maps.event.addListener(markers, 'click', (function (markers, i) {
        return function () {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, markers);
        }
      })(markers, i));

    }
    return markers
    // Append the 'script' element to 'head'
  }
  document.head.appendChild(script);

  function getDirections(markers) {
    let userLocation
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(success,error)
      function success(position){
        userLocation = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        }
      }
      function error(){
        alert("Debes activar la geolocalización")
      }
    }
    //Get directions to nearest marker

    jQuery('#FINDPOS').click(() => {
      if(typeof(userLocation) === 'undefined'){
        alert("Debes permitir la localización")
      }
      else{
          let uLocation = new google.maps.LatLng(userLocation.lat,userLocation.lng)
          let distances = [];
          let closest = -1;
          if(markers.length > 0){
          for (i = 0; i < markers.length; i++) {
            var d = google.maps.geometry.spherical.computeDistanceBetween(markers[i].position, uLocation.position);
            distances[i] = d;
            if (closest == -1 || d < distances[closest]) {
              closest = i;
            }
          }
          alert('Closest marker is: ' + markers[closest].getTitle());
      }
    }
    })
  }
})

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    您的 uLocation 变量是 google.maps.LatLng 对象,它没有 .position 属性(就此而言,google.maps.Marker 对象的 .position 属性没有记录,这样会更安全在markers 上调用(记录在案的).getPosition() 方法)

    var d = google.maps.geometry.spherical.computeDistanceBetween(
              markers[i].position, uLocation.position);         
    

    应该是:

    var d = google.maps.geometry.spherical.computeDistanceBetween(
              markers[i].getPosition(), uLocation);
    

    相关问题:

    proof of concept fiddle

    代码 sn-p:

    jQuery(document).ready(function() {
      // Create the script tag, set the appropriate attributes
    
      let script = document.createElement('script');
      script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=places&libraries=geometry';
      script.defer = true;
      var map
      let infowindow
    
      window.initMap = function() {
    
        const directionsRenderer = new google.maps.DirectionsRenderer();
        const center = {
          lat: -36.561551,
          lng: -72.0954877
        }
        infowindow = new google.maps.InfoWindow();
        map = new google.maps.Map(document.getElementById('map'), {
          zoom: 5,
          center: center
        });
        let markers = setMarkers(map)
        directionsRenderer.setMap(map);
        getDirections(markers, directionsRenderer)
    
      }
      const locations = [
        ['Super Pellet Santiago', -33.458717664930084, -70.77513497336462],
        ['Super Pellet Chillan', -36.561551, -72.0954877],
        ['Super Pellet Concepción', -36.8158124, -73.0741686],
        ['Super Pellet Los Angeles', -37.4774907, -72.3245759],
        ['Super Pellet Angol', -33.80010128657071, 151.28747820854187],
        ['Super Pellet Temuco', -38.7702088, -72.6301967]
      ];
    
      function setMarkers(map) {
        let markers = []
        for (let i = 0; i < locations.length; i++) {
          markers[i] = new google.maps.Marker({
            title: locations[i][0],
            position: {
              lat: locations[i][1],
              lng: locations[i][2]
            },
            map: map
          });
          google.maps.event.addListener(markers, 'click', (function(markers, i) {
            return function() {
              infowindow.setContent(locations[i][0]);
              infowindow.open(map, markers);
            }
          })(markers, i));
    
        }
        return markers
        // Append the 'script' element to 'head'
      }
      document.head.appendChild(script);
    
      function getDirections(markers, directionsRenderer) {
        let userLocation = {
          lat: -32.8894587,
          lng: -68.8458386
        }; // Mendoza, Capital Department, Mendoza Province, Argentina (-32.8894587, -68.8458386)
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(success, error)
    
          function success(position) {
            userLocation = {
              lat: position.coords.latitude,
              lng: position.coords.longitude
            }
          }
    
          function error() {
            document.getElementById('output').innerHTML = "Debes activar la geolocalización<br>defaulting to:Mendoza, Capital Department, Mendoza Province, Argentina (-32.8894587, -68.8458386)";
          }
        }
        //Get directions to nearest marker
    
        jQuery('#FINDPOS').click(() => {
          if (userLocation === 'undefined') {
            alert("Debes permitir la localización")
          } else {
            let uLocation = new google.maps.LatLng(userLocation.lat, userLocation.lng);
            let distances = [];
            let closest = -1;
            if (markers.length > 0) {
              for (i = 0; i < markers.length; i++) {
                var d = google.maps.geometry.spherical.computeDistanceBetween(markers[i].getPosition(), uLocation);
                distances[i] = d;
                if (closest == -1 || d < distances[closest]) {
                  closest = i;
                }
              }
              document.getElementById('output').innerHTML = 'Closest marker is: ' + markers[closest].getTitle();
              calculateAndDisplayRoute(uLocation, markers[closest].getPosition(), directionsRenderer);
            }
          }
        })
      }
    })
    
    function calculateAndDisplayRoute(start, end, directionsRenderer) {
      const directionsService = new google.maps.DirectionsService();
      directionsService.route({
          origin: start,
          destination: end,
          travelMode: google.maps.TravelMode.DRIVING,
        },
        (response, status) => {
          if (status === "OK") {
            directionsRenderer.setDirections(response);
          } else {
            window.alert("Directions request failed due to " + status);
          }
        }
      );
    }
    /* Always set the map height explicitly to define the size of the div
           * element that contains the map. */
    
    #map {
      height: 90%;
    }
    
    
    /* Optional: Makes the sample page fill the window. */
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <!DOCTYPE html>
    <html>
    
    <head>
      <title>Simple Map</title>
      <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
      <!-- jsFiddle will insert css and js -->
    </head>
    
    <body>
      <input id="FINDPOS" value="FINDPOS" type="button" />
      <div id="output"></div>
      <div id="map"></div>
    </body>
    
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      相关资源
      最近更新 更多