gpt4 book ai didi

javascript - Google map 地理编码多条线

转载 作者:行者123 更新时间:2023-11-28 21:03:49 33 4
gpt4 key购买 nike

我正在尝试使用 Google Maps JavaScript API 从多个位置对绘制多条测地折线。我在这里找到了 98% 的内容(http://stackoverflow.com/questions/2994424/google-maps-geocoding-address-to-glatlng),但我只是不知道如何添加额外的点以及其他线路(例如还显示美国芝加哥和美国洛杉矶之间的线路)。感谢任何人都可以提供的帮助。

最佳答案

新更新,此更新将目标点编码放入单独的函数中,并独立进行每个地理编码调用。应该比尝试嵌套回调过程更具可扩展性。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps API Geocoding Demo</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 1280px; height: 1024px;"></div>

<script type="text/javascript">
//add locations
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(35.00, -25.00),
mapTypeId: google.maps.MapTypeId.TERRAIN
});

var address1 = '60033';

var gc = new google.maps.Geocoder();
gc.geocode({'address': address1}, function (res1, status) {

var hub = res1[0].geometry.location;
new google.maps.Marker({
position: res1[0].geometry.location,
map: map
});

geocodeLine(hub, '44145');
geocodeLine(hub, '03103');
geocodeLine(hub, '30236');
geocodeLine(hub, '18106');
geocodeLine(hub, '64147');
geocodeLine(hub, '86401');
geocodeLine(hub, '75110');
geocodeLine(hub, '56001');
geocodeLine(hub, '80239');
geocodeLine(hub, '95776');
});

function geocodeLine(hub, address)
{
var gc = new google.maps.Geocoder();

gc.geocode({'address': address}, function (res, status) {
if (status == google.maps.GeocoderStatus.OK) {

new google.maps.Marker({
position: res[0].geometry.location,
map: map
});

new google.maps.Polyline({
path: [
hub,
res[0].geometry.location
],
strokeColor: '#FF0000',
geodesic: true,
map: map
});
}
});
}
</script>
</body>
</html>

关于javascript - Google map 地理编码多条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10344614/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com