gpt4 book ai didi

javascript - 谷歌地图地理编码(地址到 GLatLng)

转载 作者:行者123 更新时间:2023-11-29 15:05:54 25 4
gpt4 key购买 nike

我正在尝试使用 Google Maps JavaScript API 从两个 address 点绘制测地线折线。

var polyOptions = {geodesic:true};
var polyline = new GPolyline([
new GLatLng(),
new GLatLng()
], "#f36c25", 5, 0.8, polyOptions
);

map.addOverlay(polyline);

if (GBrowserIsCompatible()) {
map.addOverlay(polyline);
}

有人能告诉我如何将地址动态地理编码为 GLatLng 坐标吗?看完有点懵Google's API documentation .

最佳答案

您可能想查看以下示例。首先,它尝试对 address1 进行地理编码。如果成功,它会尝试对 address2 进行地理编码。如果两者都成功,它会在两个坐标和两个标记之间绘制测地线折线:

<!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: 500px; height: 400px;"></div>

<script type="text/javascript">
var address1 = 'London, UK';
var address2 = 'New York, US';

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(35.00, -25.00),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var gc = new google.maps.Geocoder();

gc.geocode({'address': address1}, function (res1, status) {
if (status == google.maps.GeocoderStatus.OK) {
gc.geocode({'address': address2}, function (res2, status) {
if (status == google.maps.GeocoderStatus.OK) {
new google.maps.Marker({
position: res1[0].geometry.location,
map: map
});

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

new google.maps.Polyline({
path: [
res1[0].geometry.location,
res2[0].geometry.location
],
strokeColor: '#FF0000',
geodesic: true,
map: map
});
}
});
}
});

</script>
</body>
</html>

截图:

Google Maps API Geocoding Demo

关于javascript - 谷歌地图地理编码(地址到 GLatLng),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2994424/

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