javascript - Best way to import coordinates from .gpx file and display using Google Maps API -
i mountain biker , track rides on samsung s3 galaxy
using programs such endomondo
, strava
. regarding ride saved on these 2 websites.
i have own personal website display mountain routes in various areas stay. route data recorded via gps
using endomondo , strava have exported .gpx
file. need data in .gpx file display on own personal website. started solution using google maps api
, importing .gpx file without using external tool.
i struggled find answer. came across post guy uses jquery
extract data in xml file , display data on google map: http://www.jacquet80.eu/blog/post/2011/02/display-gpx-tracks-using-google-maps-api
this how implemented html markup:
<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 own style here path: points, strokecolor: "#ff00aa", strokeopacity: .7, strokeweight: 4 }); poly.setmap(map); // fit bounds track map.fitbounds(bounds); } }); } google.maps.event.adddomlistener(window, 'load', initialize); </script>
it works. correct way it? there better way implement this?
if use postgresql database, i'd suggest use postgis , import records database. can generate kml files (st_askml) , display them on google map. if gpx huge, can use st_simplify on database query page loaded faster , still have full detailed route in database.
you have lot of possibilities:
- search rides in specified area
- measure total distance in month
- and more
Comments
Post a Comment