javascript - 使用 jQuery 在 Google map 上的两点之间绘制一条线?

标签 javascript jquery google-maps google-maps-api-3

如何使用 jQuery 或 Javascript 通过 Google map API 在两点(纬度和经度)之间绘制一条线?我需要两点之间的最短路径。它可以是任何类型的线。

最佳答案

这是绘制线条的 API v3 方法。

var line = new google.maps.Polyline({
    path: [
        new google.maps.LatLng(37.4419, -122.1419), 
        new google.maps.LatLng(37.4519, -122.1519)
    ],
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 10,
    map: map
});

这只是在两点之间绘制一条直线。如果您希望它成为最短路径,您需要考虑地球是弯曲的事实,并绘制一条测地线。为此,您必须通过添加此可选libraries参数来使用Google Maps API中的geometry库:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry"></script>

然后只需将 geodesic: true 添加到折线的选项中即可:

var line = new google.maps.Polyline({
    path: [new google.maps.LatLng(37.4419, -122.1419), new google.maps.LatLng(37.4519, -122.1519)],
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 10,
    geodesic: true,
    map: map
});

关于javascript - 使用 jQuery 在 Google map 上的两点之间绘制一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433648/

相关文章:

javascript - 我正在尝试制作具有相同标题的幻灯片

javascript - 在 Chrome 设备模式下拖动结束之前,Google map 拖动事件不会捕获中心更改

javascript - highstock,如何防止图表重新缩放

javascript - 使用 $(document.createElement()) 时是否需要使用 $(document).ready() ?

jquery - HoverIntent jQuery 并调用 jQuery 脚本

javascript - 从弹出窗口内容 Bootstrap ajax 调用

java - 如何在 Android 版 Google map 上显示路线

ios - 有关使用 TableView 在 Swift 中构建数据以及在 Google map 上进行映射的建议

javascript - Firestore云函数: Getting "ReferenceError: firebase is not defined"

javascript - HTML5 和 jQuery LocalStorage