javascript - Google maps geolocation.geolocate gives map, but not directions -
so i've made piece of code turns ordinary address geolocation , gives map , directions gps location (phonegap position, html5 location or whatever latlng). function gets called on success of phonegap or html5 location.
this code works fine in browser fails a: error: typeerror: 'undefined' not object in file://bla bla @ line 761
line 761 = geocoder.geocode ( { 'address': address }, function(results, status) {
so i've checked variables , not empty in online version , return div exists. i'm guessing there wrong in geolocator link google.
the funny thing though, shows map not directions. , marks location need go, not are. if directions doesnt load or something.
var directiondisplay; var directionsservice = new google.maps.directionsservice(); function bring_me_back(call_back_map,call_back_directions,address,position) { var latlng = new google.maps.latlng(position.coords.latitude,position.coords.longitude); // location directionsdisplay = new google.maps.directionsrenderer(); var myoptions = { zoom: 14, center: latlng, maptypeid: google.maps.maptypeid.roadmap, maptypecontrol: true }; var map = new google.maps.map(document.getelementbyid(call_back_map),myoptions); directionsdisplay.setmap(map); directionsdisplay.setpanel(document.getelementbyid(call_back_directions)); var marker = new google.maps.marker({ position: latlng, map: map, title:"my location" }); // find address var geocoder = new google.maps.geocoder (map); geocoder.geocode ( { 'address': address }, function(results, status) { if (status == google.maps.geocoderstatus.ok) { var end = results [0].geometry.location; map.setcenter(results [0].geometry.location); marker.setposition(results [0].geometry.location); var start = latlng; // gps location var request = { origin:start, destination:end, travelmode: google.maps.directionstravelmode.walking }; directionsservice.route(request, function(response, status) { if (status == google.maps.directionsstatus.ok) { directionsdisplay.setdirections(response); } else alert("directions not successful following reason: " + status); }); } else { $('#'+call_back).html("geocode not successful following reason: " + status); } }); } the way call google (it has plenty of time load , "initialize" gets called way before use code).
function loadgooglescript() { if(!(navigator.network.connection.type == connection.none)) { var script = document.createelement('script'); script.type = 'text/javascript'; script.src = 'http://maps.google.com/maps/api/js?v=3.6&sensor=false&callback=initialize'; document.body.appendchild(script); return true; } else { return false; } }
the feedback api way limited, can't make more of it.
after hell of search, found out 1 variable:
var directionsservice = new google.maps.directionsservice(); was called before google script loaded. changed
var directionsservice; and called:
directionsservice = new google.maps.directionsservice(); from inside the function.
thanks help.
Comments
Post a Comment