gpt4 book ai didi

javascript - 从 .gpx 文件导入坐标并使用 Google Maps API 显示的最佳方式

转载 作者:太空狗 更新时间:2023-10-29 13:31:42 26 4
gpt4 key购买 nike

我是一名山地车手,我使用 EndomondoStrava 等程序在我的 Samsung S3 Galaxy 上跟踪我的骑行。关于我的旅程的所有信息都保存在这两个网站上。

我有自己的个人网站,上面展示了我所住的各个地区的山区路线。使用 Endomondo 和 Strava 通过 GPS 记录的路线数据我已导出到 .gpx 文件。我需要 .gpx 文件中的这些数据显示在我自己的个人网站上。因此,我开始寻找使用 Google Maps API 并在不使用外部工具的情况下导入 .gpx 文件的解决方案。

我努力寻找答案。我看到这篇文章,其中有人使用 jQuery 提取 XML 文件中的数据并在他的 Google map 上显示这些数据: http://www.jacquet80.eu/blog/post/2011/02/Display-GPX-tracks-using-Google-Maps-API

这是将它实现到我的 HTML 标记中的方式:

<script>
function initialize() {
var route1Latlng = new google.maps.LatLng(-33.7610590,18.9616790);
var mapOptions = {
center: route1Latlng,
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

$.ajax({
type: "GET",
url: "gpx/my_route.gpx",
dataType: "xml",
success: function (xml) {
var points = [];
var bounds = new google.maps.LatLngBounds();
$(xml).find("trkpt").each(function () {
var lat = $(this).attr("lat");
var lon = $(this).attr("lon");
var p = new google.maps.LatLng(lat, lon);
points.push(p);
bounds.extend(p);
});
var poly = new google.maps.Polyline({
// use your own style here
path: points,
strokeColor: "#FF00AA",
strokeOpacity: .7,
strokeWeight: 4
});
poly.setMap(map);
// fit bounds to track
map.fitBounds(bounds);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

它有效。但这是正确的做法吗?有没有更好的方法来实现它?

最佳答案

2020 年更新

适用于 Google map 最新 API:

<!DOCTYPE html>
<html>
<head>
<title>Add Map</title>
<script
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=drawing&v=weekly"
defer
></script>
<style type="text/css">
#map {
height: 400px;
width: 400px;
}
</style>
<script>
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 3,
center: { lat: 0, lng: -180 },
mapTypeId: "satellite",
disableDefaultUI: true,
});

fetch('2020-10-12_2007.gpx')
.then(response => response.text())
.then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
//.then(data => console.log(data))
.then(doc =>
{
var points = [];
var bounds = new google.maps.LatLngBounds();

const nodes = [...doc.getElementsByTagName('trkpt')];
nodes.forEach(node =>
{
var lat = node.getAttribute("lat");
var lon = node.getAttribute("lon");
//console.log(lat);

var p = new google.maps.LatLng(lat, lon);
points.push(p);
bounds.extend(p);
})

var poly = new google.maps.Polyline({
path: points,
strokeColor: "#0000FF",
strokeOpacity: 1,
strokeWeight: 4
});
poly.setMap(map);
// fit bounds to track
map.fitBounds(bounds);
})
}
</script>
</head>
<body>
<h3>My Google Maps Demo</h3>
<!--The div element for the map -->
<div id="map"></div>
</body>
</html>

关于javascript - 从 .gpx 文件导入坐标并使用 Google Maps API 显示的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15829048/

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