javascript - google maps api, directions from 2 points -


i have problem google map's api. have add site map show position of user , route point prefixed.

1) can't give "origin" of request, variable pos geolocalization.

2) can't give auto zoom coorect distance looking @ 2 marker.

this code:

var directionsservice = new google.maps.directionsservice(); var directionsdisplay = new google.maps.directionsrenderer();  var request = {     travelmode: google.maps.directionstravelmode.driving   };  //initializing map var initialize = function() {    // taking address of school   var address = document.getelementbyid('address').firstchild.nodevalue;   var city = document.getelementbyid("city").firstchild.nodevalue;   var comun = document.getelementbyid("comun").firstchild.nodevalue;   var search = address + " " + city + " " + comun;     // initializing geocoder , searcing address in search   var geocoder = new google.maps.geocoder();   geocoder.geocode( {'address': search}, function(results,status) {     if (status == google.maps.geocoderstatus.ok) {       var marker2 = new google.maps.marker({          position: results[0].geometry.location,         map: map,         title: 'posizione scuola'       });     } else {       alert("problema nella ricerca dell'indirizzo: " + status);     }   });    // input visualization options   var options = {      zoom: 12,     maptypeid: google.maps.maptypeid.roadmap,     disabledefaultui: true   };    // create map   var map = new google.maps.map(document.getelementbyid('maps'), options);    // try html5 geolocation   if(navigator.geolocation) {     navigator.geolocation.getcurrentposition(function(position) {       var pos = new google.maps.latlng(position.coords.latitude, position.coords.longitude);       map.setcenter(pos);       request.origin = (map.center);       // put marker       var marker = new google.maps.marker({          position: pos,         map: map,         title: 'tua posizione'       });     });   } else {     alert("il tuo dispositivo non permette di visualizzare la tua posizione");   }     directionsdisplay.setmap(map);   calcroute(); } // end initialize  var calcroute =function() {    // taking address of school   var address = document.getelementbyid('address').firstchild.nodevalue;   var city = document.getelementbyid("city").firstchild.nodevalue;   var comun = document.getelementbyid("comun").firstchild.nodevalue;   var search = address + " " + city + " " + comun;     request.destination = search;    directionsservice.route(request, function(response, status) {     if (status == google.maps.directionsstatus.ok) {       directionsdisplay.setdirections(response);     }   }); }  window.onload = initialize; 

thank help

1.) must move call of calcroute success-callback of getcurrentposition (geolocating asynchronous process, request.origin not set yet when call calcroute)

if(navigator.geolocation) {     navigator.geolocation.getcurrentposition(function(position) {       var pos = new google.maps.latlng(position.coords.latitude, position.coords.longitude);       map.setcenter(pos);       request.origin = (pos);       // put marker       var marker = new google.maps.marker({          position: pos,         map: map,         title: 'tua posizione'       });       directionsdisplay.setmap(map);       calcroute();     });   } else {     alert("il tuo dispositivo non permette di visualizzare la tua posizione");   }  } 

2.) work when have fixed 1.)


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -