我正在使用Google Maps JS API搜索附近的地方,即基于我的LatLng的餐馆:
var request = {
        location: myLocation,rankBy: google.maps.places.RankBy.disTANCE,types: ['bar','cafe','food','liquor_store','lodging','meal_delivery','meal_takeaway','night_club','restaurant'],keyword: ['bar','pub']
    };
searchService.nearbySearch(request,callback);

我得到了结果数组,想要显示从数组到第一个位置的方向:

var request = {
        origin: myLocation,destination: bars[0].geometry.location,travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request,function (response,status) {
        if (status == google.maps.Directionsstatus.OK) {
            directionsdisplay.setDirections(response);
            directionsdisplay.setoptions({
                suppressMarkers: true
            });
            var myRoute = response.routes[0].legs[0];
            for (var i = 0; i < myRoute.steps.length; i++) {
                Map.marker(myRoute.steps[i].start_location,myRoute.steps[i].instructions);
            }
        } else {
            console.log("directionsService : " + status);
        }
    });

其中bars [0]是包含searchService.nearbySearch查询结果的数组.

我确实得到了指示,但即使正确放置了引脚,最后的“虚线腿”也似乎丢失了.当您将其与maps.google.com指示进行比较时,引脚和方向路线之间会出现虚线.

我的API说明:http://damianbilski.com/temp/api_example.png

Maps.google.com路线:http://damianbilski.com/temp/online_example.png

任何想法如何使用directionsService.route获得最后一个虚线的腿.
非常感谢你的帮助!

解决方法

Google Maps Javascript API v3路线服务不会为您执行此操作(至少目前为止).如果需要,可以从路线结果的末尾添加“虚线”折线到地点的位置.

proof of concept fiddle

代码段:

var geocoder;
var map;
var searchService;
var myLocation;
var directionsService = new google.maps.DirectionsService();
var directionsdisplay = new google.maps.DirectionsRenderer();

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
    });
  myLocation = map.getCenter();
  var marker = new google.maps.Marker({
    position: myLocation,map: map
  });
  searchService = new google.maps.places.PlacesService(map);
  directionsdisplay.setMap(map);
  var request = {
    location: myLocation,'pub']
  };
  searchService.nearbySearch(request,function(bars,status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
      var barMark = new google.maps.Marker({
        position: bars[0].geometry.location,map: map,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)
        }
      });
      var request = {
        origin: myLocation,travelMode: google.maps.TravelMode.WALKING
      };
      directionsService.route(request,function(response,status) {
        if (status == google.maps.Directionsstatus.OK) {
          directionsdisplay.setDirections(response);
          directionsdisplay.setoptions({
            suppressMarkers: true,preserveViewport: true
          });
          var polyline = getpolyline(response);
          map.setCenter(polyline.getPath().getAt(polyline.getPath().getLength() - 1));
          map.setZoom(19);

          var lineLength = google.maps.geometry.spherical.computedistanceBetween(bars[0].geometry.location,polyline.getPath().getAt(polyline.getPath().getLength() - 1));
          var lineheading = google.maps.geometry.spherical.computeheading(bars[0].geometry.location,polyline.getPath().getAt(polyline.getPath().getLength() - 1));
          var markerO = new google.maps.Marker({
            position: google.maps.geometry.spherical.computeOffset(bars[0].geometry.location,lineLength * 0.1,lineheading)
          });
          var markerD = new google.maps.Marker({
            position: google.maps.geometry.spherical.computeOffset(bars[0].geometry.location,lineLength * 0.9,lineheading)
          });

          var markerA = new google.maps.Marker({
            position: google.maps.geometry.spherical.computeOffset(markerO.getPosition(),lineLength / 3,lineheading - 40)
          });
          var markerB = new google.maps.Marker({
            position: google.maps.geometry.spherical.computeOffset(markerD.getPosition(),lineheading - 140)
          });

          var curvedLine = new GmapsCubicBezier(markerO.getPosition(),markerA.getPosition(),markerB.getPosition(),markerD.getPosition(),0.01,map);

          var line = new google.maps.polyline({
            path: [bars[0].geometry.location,polyline.getPath().getAt(polyline.getPath().getLength() - 1)],strokeOpacity: 0,icons: [{
              icon: {
                path: 'M 0,-1 0,1',strokeOpacity: 1,scale: 4
              },offset: '0',repeat: '20px'
            }],// map: map
          });
        } else {
          console.log("directionsService : " + status);
        }
      });
    }
  });
}
google.maps.event.addDomListener(window,"load",initialize);

function getpolyline(result) {
  var polyline = new google.maps.polyline({
    path: []
  });
  var path = result.routes[0].overview_path;
  var legs = result.routes[0].legs;
  for (i = 0; i < legs.length; i++) {
    var steps = legs[i].steps;
    for (j = 0; j < steps.length; j++) {
      var nextSegment = steps[j].path;
      for (k = 0; k < nextSegment.length; k++) {
        polyline.getPath().push(nextSegment[k]);
      }
    }
  }
  return polyline;
}

var GmapsCubicBezier = function(latlong1,latlong2,latlong3,latlong4,resolution,map) {
  var lat1 = latlong1.lat();
  var long1 = latlong1.lng();
  var lat2 = latlong2.lat();
  var long2 = latlong2.lng();
  var lat3 = latlong3.lat();
  var long3 = latlong3.lng();
  var lat4 = latlong4.lat();
  var long4 = latlong4.lng();

  var points = [];

  for (it = 0; it <= 1; it += resolution) {
    points.push(this.getBezier({
      x: lat1,y: long1
    },{
      x: lat2,y: long2
    },{
      x: lat3,y: long3
    },{
      x: lat4,y: long4
    },it));
  }
  var path = [];
  for (var i = 0; i < points.length - 1; i++) {
    path.push(new google.maps.LatLng(points[i].x,points[i].y));
    path.push(new google.maps.LatLng(points[i + 1].x,points[i + 1].y,false));
  }

  var Line = new google.maps.polyline({
    path: path,geodesic: true,strokeOpacity: 0.0,icons: [{
      icon: {
        path: 'M 0,scale: 4
      },repeat: '20px'
    }],strokeColor: 'grey'
  });

  Line.setMap(map);

  return Line;
};


GmapsCubicBezier.prototype = {

  B1: function(t) {
    return t * t * t;
  },B2: function(t) {
    return 3 * t * t * (1 - t);
  },B3: function(t) {
    return 3 * t * (1 - t) * (1 - t);
  },B4: function(t) {
    return (1 - t) * (1 - t) * (1 - t);
  },getBezier: function(C1,C2,C3,C4,percent) {
    var pos = {};
    pos.x = C1.x * this.B1(percent) + C2.x * this.B2(percent) + C3.x * this.B3(percent) + C4.x * this.B4(percent);
    pos.y = C1.y * this.B1(percent) + C2.y * this.B2(percent) + C3.y * this.B3(percent) + C4.y * this.B4(percent);
    return pos;
  }
};
html,body,#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map_canvas"></div>

javascript – Google Maps API directionsService.route与Google Maps Directions不同的更多相关文章

  1. HTML5地理定位_动力节点Java学院整理

    地理位置(Geolocation)是 HTML5 的重要特性之一,提供了确定用户位置的功能,借助这个特性能够开发基于位置信息的应用。今天这篇文章向大家介绍一下 HTML5 地理位置定位的基本原理及各个浏览器的数据精度情况

  2. 适用于iOS和路线的Google Maps SDK

    解决方法库中有一些东西不在文档中,所以如果你正在寻找一个功能,那么值得下载SDK并查看标题.然而,在当前版本1.0.2中,我没有看到任何路由–搜索路线或绘制路线.目前,您唯一的选择可能是使用其他GoogleApi来查找路径,然后正如Lee所说,使用折线绘制它们.

  3. ios – didUpdateLocations从未调用过

    我正在尝试获取用户的位置.为此,我在info.plist中设置了以下属性:我还在viewDidLoad方法中添加了以下代码以及下面的函数.问题是locationManager(manager,didUpdate…

  4. ios – 重命名并重写为Swift后对象解码崩溃

    由于我们已经重命名了(Bestemming–>Place)类并将其从Objective-c重写为Swift,因此一些用户会遇到崩溃.我们正在尝试使用NSCoding原则从NSUserDefaults加载对象.碰撞:班级:从NSUserDefaults阅读:崩溃日志说它在第0行崩溃,这是注释所以我认为它在init方法中崩溃,我认为它与一个null为空但不能为null的对象有关.我尝试过的:>尝试在S

  5. 适用于iOS的Google Maps SDK不断增加内存使用量

    我已经构建了一个在地图上显示标记的简单应用程序,我从服务器的JSON文件加载其x,y,标记是可点击的,所以一旦你在任何标记上它将你带到另一个UIViewController(我们将它命名为BViewController).我已经监视了内存使用情况,所以每次我从BViewController返回到MapViewController(里面的地图)时,它只是内存使用量的两倍我尝试将其设置为nill或从s

  6. 通过cocoapods安装适用于iOS的Google Maps SDK会导致链接器错误

    我正在尝试使用cocoapods安装适用于iOS版本1.3.0的GoogleMapsSDK.实际上安装过程很成功,但是当我尝试使用框架时,app构建过程会返回链接器错误,例如:由于某种原因,链接器无法找到框架.我的cocoapods版本是0.20.1.XCode4.6.2.除了谷歌地图SDK,我的项目还有另外两个通过cocoapods添加的库.这是我的Podfile:提前致谢.解决方法您应该看到一个文件“Pods.xcconfig”,其中包含pod正在使用的框架.将应用程序的目标配置更改为基于CocoaP

  7. ios – 未提示在应用程序中启用位置服务

    更新:这不是重复.我已经在info.plist中添加了所需的密钥,如我原始问题中所述,问题仍然存在.我已经尝试了各种组合的所有三个键.在任何人感到不安之前,我已阅读了许多AppleDev论坛帖子和堆栈溢出帖子,无法弄清楚为什么我的应用程序拒绝提示用户允许使用时授权.我已将以下密钥添加到我的Info.plist文件中,并附带一个String值:然后我写了(在Swift和Obj-C中)应该提示用户的代

  8. ios – 在UIViewController显示为3DTouch预览时检测UITouches

    是否有可能从UIViewController检测触摸并获取触摸的位置,UIViewController当前用作3DTouch的previewingContext视图控制器?

  9. 在我的iOS应用中实施新的Google Maps SDK

    没有玷污测试它,但似乎是正确的.他们给我发了this个网页.祝好运!.您可能必须从项目中删除对armv7s的支持.

  10. ios – Google商家信息自动填充功能 – 如何获得纬度和经度?

    解决方法仅使用此URL不可能:我需要做的就是从响应中获取place_id,然后在NEXT以下URL中使用它:哪里:PLACE_ID–从先前的请求中检索到的place_id.API_KEY–Google生成的密钥,用于我的应用.必须使用上述网址中的详细信息替换自动填充功能.

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部