I am exploring Google maps APIs. I have a web service which returns the GeoJSON object in response. I want to render it on the Google maps. I tried below API;
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
This gives us the GeoJSON for given start and end in request parameter. I am trying to get the GeoJSON response from my service and instead of Google data, I am trying to render my own response.
The data returned from my custom service is in the same format as Google.
The data returned from Google service is in the form like
I have constructed the object same is Google DirectionsService Response.
Please check details below;
https://developers.google.com/maps/documentation/javascript/directions#DirectionsResults
"routes":[{"bounds":{"northeast":{"lat":30.2844454,"lng":-97.7040698},"southwest":{"lat":30.2121885,"lng":-97.7506593}},"copyrights":"Map
data ©2014 Google","legs":.... Steps....}
EDIT:
I tried another option with addGeoJson() API as;
function loadGeoJsonString(geoString) {
var geojson = JSON.parse(geoString);
map.data.addGeoJson(geojson);
zoom(map);
}
JSON string which I am using is validated by jsonlint.
Something like this?
directionsDisplay = new google.maps.DirectionsRenderer();
var Basingstoke = new google.maps.LatLng(51.2949612, -1.0643864);
var mapOptions = {
zoom:7,
center: Basingstoke
}
map = new google.maps.Map(document.getElementById('map'), mapOptions);
directionsDisplay.setMap(map);
var point1 = new google.maps.LatLng(51.2941293,-0.9139252);
var point2 = new google.maps.LatLng(51.3250339,-0.8050919);
var wps = [{ location: point1 }, { location: point2 }];
var org = new google.maps.LatLng ( 51.2949612, -1.0643864);
var dest = new google.maps.LatLng ( 52.3069282, -0.7540226);
var request = {
origin: org,
destination: dest,
waypoints: wps,
durationInTraffic: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var newRoute = response.routes[0].legs[2].duration.value;
directionsDisplay.setDirections(response);
alert ('Travel Time: ' + newRoute + ' seconds');
}
else
alert ('failed to get directions');
});
}
google.maps.event.addDomListener(window, 'load', initialize);
Related
I need to call pass params to calcRoute() on click, what I am doing is:
function calcRoute(source,destt) {
var start = new google.maps.LatLng(source);
var end = new google.maps.LatLng(destt);
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
Click Event:
$('#go').click(function(){
var from= "24.942834,67.121237";
var to = "24.944908,67.124491";
calcRoute(from,to);
});
It's returning the status with ZERO_RESULT
It was working fine when I hard coded lat n lng in calcRoute(), i.e
var start = new google.maps.LatLng(24.942834,67.121237);
var end = new google.maps.LatLng(24.944908,67.124491);
Thanks for any Help.
Direct string will not work as LatLng object you have to convert "24.942834,67.121237" to google.maps.LatLng to make it work.
$('#go').click(function(){
var from= new google.maps.LatLng(24.942834,67.121237);// convert it to latlng object
var to = new google.maps.LatLng(24.944908,67.124491);
calcRoute(from,to);
});
if you have "24.942834,67.121237" then you can use it like this:
var str="24.942834,67.121237";
var latlng=str.split(',')
var to = new google.maps.LatLng(parseFloat(latlng[0]), parseFloat(latlng[1]));
I have some problems with upgrading google maps v2 in v3.
Follow code is v2:
function routing(target, width, lenght, name, zip, city, street) {
if (GBrowserIsCompatible()) {
if (counter > 0) {directions.clear()};
counter++;
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
directionsPanel = document.getElementById("route");
directions = new GDirections(map, directionsPanel);
var startpoint = name+', '+zip+' '+city+', '+street
if (target == 'messe') {
directions.load("from: "+startpoint+"#"+width+","+lenght+" to: Messe, 50679 #T_koeln#, Deutz-Mülheimer Straße 40#50.9473327,6.98312820000001");
};
if (target == 'airport') {
directions.load("from: "+startpoint+"#"+width+","+lenght+" to: Flughafen Koeln, 50679 #T_koeln#, Kennedystraße#50.8782914,7.122399800000039");
};
}
}
here is the v3 code:
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
function routing(target, width, lenght, name, zip, city, street) {
if (counter > 0) {directionsDisplay.setMap(null)};
counter++;
var mapOptions = {
zoom: #zoomfaktor_detail#,
center: new google.maps.LatLng(width, length),
overviewMapControl: true,
overviewMapControlOptions:{opened:true},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"),mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("route"));
var startpoint = name+', '+zip+' '+city+', '+street
if (target == 'messe') {
end = 'Messe, 50679 #T_koeln#, Deutz-Mülheimer Straße 40#50.9473327,6.98312820000001';
start = startpoint+"#"+width+","+lenght;
request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
}
if (target == 'airport') {
end = 'Flughafen Koeln, 50679 #T_koeln#, Kennedystraße#50.8782914,7.122399800000039';
start = startpoint+"#"+width+","+lenght;
request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
}
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
Why does it not function? Has someone an idea?
It isn't working because the route can't be found and the directions service is returning an error which is (or was) being silently ignored.
I suspect the problem is the "#" in your start and end addresses. Remove them (and the text after them), give the directions service valid addresses (or give it the coordinates as a google.maps.LatLng object).
i am new in sencha touch.
i have integrated google map using sencha touch and display marker on user current location.
now, i want to display direction from source address to destination address on map.
the sample like this,
https://maps.google.com/maps?saddr=21.220901,72.829056&daddr=20.915907,72.871628
i want display only map view with direction path.
how to display direction between two places on goole map using sencha toch ?
here is code,
function directionMap(src,dest){
// alert('calling');
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var map = new google.maps.Map(document.getElementById('map'), {
zoom:10,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
directionsDisplay.setMap(map);
var request = {
// origin: '21.220901,72.829056',
// destination: '20.915907,72.871628',
origin: src,
destination: dest,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
just call directionMap function with source and destination it will draw path on map.
I'm trying to load markers from XML file on a map used for outputing directions. Basically, it's the combination of two demos found on Google's documentation pages.
Directions: https://google-developers.appspot.com/maps/documentation/javascript/examples/directions-panel
XML: http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/downloadurl_info.html
I have first created the directions map and then tried to add XML file that contains markers.
I'm probably making a simple mistake, but since I'm not good with js and coding, can't find what. There are no errors displayed, only a blank page.
Here is my current code:
<script>
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(41.850033, -87.6500523)
};
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directions-panel'));
var control = document.getElementById('control');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP].push(control);
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
downloadUrl("http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/moredata.xml", function(data) {
var markers = data.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = createMarker(markers[i].getAttribute("name"), latlng);
}
});
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
Here is the (non-working) jsFiddle: http://jsfiddle.net/ajJ3u/
Problems from a quick review:
you are creating the map twice.
you don't have a createMarker function. If that call came from one of the examples, you missed bringing it to the new map.
downloadUrl is subject to a cross-domain security restriction. If your page is not running in the "http://gmaps-samples-v3.googlecode.com" domain, it won't work. You need to access xml from the same domain as the page is running in or use a proxy.
Example of directions from/to markers from xml (translated to v3 from Mike Williams' v2 tutorial
I am trying to display multiple routes on same map but am unable to do so.
No matter what I do I get only one route.
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
Any pointers will be helpful.
I had the same problem. This thread on a Google group for Google maps shows how to do it.
The author (a Google employee), wrote this:
You should be able to create two DirectionsRenderer objects, each that
use the same map and different DirectionsResults.
var map = new google.maps.Map(document.getElementById("map_canvas"));
function renderDirections(result) {
var directionsRenderer = new google.maps.DirectionsRenderer;
directionsRenderer.setMap(map);
directionsRenderer.setDirections(result);
}
var directionsService = new google.maps.DirectionsService;
function requestDirections(start, end) {
directionsService.route({
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result) {
renderDirections(result);
});
}
requestDirections('Huntsville, AL', 'Boston, MA');
requestDirections('Bakersfield, CA', 'Vancouver, BC');
I tried it and it does work.
Did you try as follows?
Here I have captured one path and displayed it. You can do the same by writing pointsArray = result.routes[1].overview_path; besides it and display it in a new loop.
directionsService.route (request, function (result, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections (result);
pointsArray = result.routes[0].overview_path;
var i = 0;
var j = 0;
for (j = 0; j < pointsArray.length; j++)
{
var point1 = new google.maps.Marker ({
position:pointsArray [j],
draggable:false,
map:map,
flat:true
});
}
}
});