javascript - Google Maps API: Combining Directions Service with watchPosition Geolocation -
i'm trying display map using google's directions service origin, destination, , blue directions line connecting two. however, trying use watchposition origin lat/lng.
so far displays correctly, except green origin marker doesn't move watchposition updates new lat/lng coordinates. i'm trying possible? or have create third marker follows user's coordinates?
here have in script:
var directionsdisplay; var directionsservice = new google.maps.directionsservice(); var map; function initialize() { directionsdisplay = new google.maps.directionsrenderer(); var mapoptions = { zoom:7, center: new google.maps.latlng(38.5815719, -121.4943996) } map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); directionsdisplay.setmap(map); calcroute(); } function handlenogeolocation(errorflag) { if (errorflag) { var content = 'error: geolocation service failed.'; } else { var content = 'error: browser doesn\'t support geolocation.'; } var options = { map: map, position: new google.maps.latlng(<%= @order.origin_lat %>, <%= @order.origin_lng %>), content: content }; var infowindow = new google.maps.infowindow(options); map.setcenter(options.position); } function calcroute() { if(navigator.geolocation) { navigator.geolocation.watchposition(function(position) { var pos = new google.maps.latlng(position.coords.latitude, position.coords.longitude); var request = { origin: pos, destination: new google.maps.latlng(<%= @order.user.latitude %>, <%= @order.user.longitude %>), travelmode: google.maps.travelmode.driving }; directionsservice.route(request, function(response, status) { if (status == google.maps.directionsstatus.ok) { directionsdisplay.setdirections(response); } }); }, function() { handlenogeolocation(true); }); } else { // browser doesn't support geolocation handlenogeolocation(false); } } google.maps.event.adddomlistener(window, 'load', initialize);
it doesn't accomplishable in mobile web, created third marker moves according user's position.
Comments
Post a Comment