栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Google Maps API-获取与邮政编码最接近的点

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Google Maps API-获取与邮政编码最接近的点

通常的解决方案是使用google.maps.geometry.spherical库computeDistanceBetween(from:LatLng,to:LatLng,radius?:number)方法将数字减少到10,然后使用距离矩阵将行驶距离返回到位置,因此结果可以按行驶距离(实际行进距离)进行排序,并通过请求范围内的实际行进距离缩小到最接近的3-5个位置。
(从FusionTables“比萨店”示例中借用的数据)

  function preAddress() {    var address = document.getElementById('address').value;    geoprer.geopre( { 'address': address}, function(results, status) {      if (status == google.maps.GeoprerStatus.OK) {        map.setCenter(results[0].geometry.location);    if (customerMarker) customerMarker.setMap(null);        customerMarker = new google.maps.Marker({ map: map, position: results[0].geometry.location        });    closest = findClosestN(results[0].geometry.location,10);        // get driving distance        closest = closest.splice(0,3);        calculateDistances(results[0].geometry.location, closest,3);      } else {        alert('Geopre was not successful for the following reason: ' + status);      }    });  }function findClosestN(pt,numberOfResults) {   var closest = [];   document.getElementById('info').innerHTML += "processing "+gmarkers.length+"<br>";   for (var i=0; i<gmarkers.length;i++) {     gmarkers[i].distance = google.maps.geometry.spherical.computeDistanceBetween(pt,gmarkers[i].getPosition());     document.getElementById('info').innerHTML += "process "+i+":"+gmarkers[i].getPosition().toUrlValue(6)+":"+gmarkers[i].distance.toFixed(2)+"<br>";     gmarkers[i].setMap(null);     closest.push(gmarkers[i]);   }   closest.sort(sortByDist);   return closest;}function sortByDist(a,b) {   return (a.distance- b.distance)}function calculateDistances(pt,closest,numberOfResults) {  var service = new google.maps.DistanceMatrixService();  var request =    {      origins: [pt],      destinations: [],      travelMode: google.maps.TravelMode.DRIVING,      unitSystem: google.maps.UnitSystem.METRIC,      avoidHighways: false,      avoidTolls: false    };  for (var i=0; i<closest.length; i++) request.destinations.push(closest[i].getPosition());  service.getDistanceMatrix(request, function (response, status) {    if (status != google.maps.DistanceMatrixStatus.OK) {      alert('Error was: ' + status);    } else {      var origins = response.originAddresses;      var destinations = response.destinationAddresses;      var outputDiv = document.getElementById('side_bar');      outputDiv.innerHTML = '';      var results = response.rows[0].elements;      for (var i = 0; i < numberOfResults; i++) {        closest[i].setMap(map);        outputDiv.innerHTML += "<a href='javascript:google.maps.event.trigger(closest["+i+"],"click");'>"+closest[i].title + '</a><br>' + closest[i].address+"<br>" + results[i].distance.text + ' appoximately ' + results[i].duration.text + '<br><hr>';      }    }  });}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/420732.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号