【问题标题】:Best way to import coordinates from .gpx file and display using Google Maps API从 .gpx 文件导入坐标并使用 Google Maps API 显示的最佳方式
【发布时间】:2013-03-27 13:55:53
【问题描述】:

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

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

我努力寻找答案。我遇到了这篇文章,其中那个家伙使用jQuery 来提取 XML 文件中的数据并将这些数据显示在他的 Google 地图上: 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>

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

【问题讨论】:

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


    【解决方案1】:

    2020 年更新

    可与 Google 地图的最新 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>
    

    【讨论】:

      【解决方案2】:

      如果您使用 PostgreSQL 数据库,我建议您使用PostGIS 并将您的记录导入数据库。然后您可以轻松生成 kml 文件 (ST_asKml) 并在 Google Map 上显示它们。如果您的 gpx 很大,您可以在数据库查询中使用 ST_Simplify,以便更快地加载页面,并且您的数据库中仍然有完整的详细路线。

      你也有很多可能性:

      • 搜索指定区域的游乐设施
      • 测量一个月的总距离
      • 等等

      【讨论】:

      • 我实际上在使用 SQL Server。如果我已经将 KML 文件保存为 .gpx,您为什么建议我使用它们?
      • 从数据库生成kml时,您可以轻松地在同一张地图上显示更多路线。使用 kml 样式设置路线样式也更容易。
      • Kml 是存储和显示地理数据的“google 方式”,GMaps 和 GEarth 都很好地支持导入和导出 kml 而不是 gpx。此外,使用 GPSBabel 等工具在 GPX 和 KML 之间进行转换非常自然,甚至可以使用您最喜欢的语言(python、php 等)进行破解
      • @heltonbiker Kml 虽然不是最流行的格式,但很多人对“谷歌方式”并不满意。据我了解,当发生这种转换时,您可能会丢失信息,例如环境数据等。
      猜你喜欢
      • 2021-10-21
      • 2012-07-11
      • 2014-02-13
      • 2013-03-22
      • 1970-01-01
      • 2019-08-02
      • 2012-01-30
      • 2014-05-20
      • 2015-01-02
      相关资源
      最近更新 更多