如何在谷歌地图中可视化路径

1 投票
1 回答
3787 浏览
提问于 2025-04-17 02:35

我想创建一个谷歌地图的网页应用,能够导入一个GPX文件,把路线在地图上以覆盖层的形式显示出来。导入后,我希望结果看起来像这样!

我该怎么做呢?我正在使用Python的Django框架。

希望有人能帮帮我。

1 个回答

4

我对GPX、Python和Django这方面的内容帮不了你。不过,如果你能得到一组包含你路线所有点的经纬度数据,这段代码就能帮你生成这条路线。

<!DOCTYPE html>
<html>
<head>
<title>Draw a polyline given many coordinates</title>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js"></script>

<script>
    function initialize() {
        var homeLatlng = new google.maps.LatLng(51.476706,0);
        var myOptions = {
            zoom: 15,
            center: homeLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        // create an array of coordinates
        var arrCoords = [
            new google.maps.LatLng(51.482238,0.001581),
            new google.maps.LatLng(51.473364,0.011966),
            new google.maps.LatLng(51.471974,-0.000651),
            new google.maps.LatLng(51.472108,-0.002196),
            new google.maps.LatLng(51.474995,-0.003827),
            new google.maps.LatLng(51.476492,-0.005629),
            new google.maps.LatLng(51.477855,-0.006058),
            new google.maps.LatLng(51.478443,-0.007045),
            new google.maps.LatLng(51.479298,-0.007861),
            new google.maps.LatLng(51.481202,-0.002136),
            new google.maps.LatLng(51.481577,-0.0022)
        ];

        // draw the route on the map            
        var route = new google.maps.Polyline({
            path: arrCoords,
            strokeColor: "#00FF00",
            strokeOpacity: 1.0,
            strokeWeight: 4,
            geodesic: false,
            map: map
        }); 
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
  <div id="map_canvas"></div>
</body>
</html>

撰写回答