what Am i doing wrong in performing reverse geocoding - google-maps

I am trying reverse geocoding, I have three coordinates and I want to convert them to addresses, I have used following method.
var point1 = new google.maps.LatLng(latLng1);
var point2 = new google.maps.LatLng(latLng2);
var point3 = new google.maps.LatLng(latLng3);
where latLng1, latLng2, latLng3 are coordinates.
and then further I want to use these addresses to following code to create a path
var request = {
origin:location1,
destination:location2,
waypoints:[{location: point1}, {location: point2}, {location: point3}],
travelMode: google.maps.DirectionsTravelMode.WALKING
};
but it never displays anything.
Am I doing it right, One more thing it says in google APIv3 that we can either use string of address in waypoint or latlng. how we can use latlag in waypoint.

Are you showing your whole code here?
Please, post your latLng1 value, for example.
Also, show your directionsService call. I do like that:
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else alert(status);
});
In any case - try do alert() your request statuses, directions may not be available for these points.
By the way you don't need to reverse geocode your coordinates to addresses to get directions. You can use coordinates in waypoints, which is what you doing here.

Related

Multiple directions on the same Google Map on click

I would like to reproduce a multi direction route sistem on the same Google Map like this website: http://hotelberna.com/en/where-we-are/
The same start point and multiple destinations. When I click on each final destination, then the map show me the new route.
Thanks in advance
I think you must be talking about something like this, right?
http://jsfiddle.net/dm1o1ktf/
(a modified version of the sample code in the documentation)
What you need is to trigger some function like calcRoute when you select a new location; in which you calculate the new route using the directionsService, like this:
function calcRoute() {
var start = "Mountain View, CA";
var end = document.getElementById('end').value;
var 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);
}
});
}

Report points on a street

it's a couple of days i'm struggling with this problem i cannot solve.
I have an Android application sending me a certain number of coordinates; each coordinate has a value related to it and what i need to do is to report this points on a map, visualizing different marker colors depending on this value.
I'm tryng to do it via Google maps API.
I have all the points stored in a fusion table and i'm able to build a layer with proper style, but the problem is that these points do not correspond on real streets on the map and it is obviously necessary that they do.
What i am tryng is to use the DirectionsService, as suggested here:(google maps geocoder - point on a street) and i know that gps cannot be so precise.
The way i tried til now is to put all my points in an array (without using the fusion table layer, cause i dunno how to connect the two tehings) and then for each point make a request to the DirectionsService (i know it's terribly slow, but for now i need it to be precise and if you have suggestions it's well accepted) but in this way only certain points are well represented while other have a null response from the service and are not reported at all.
Any suggestion?
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var urbino = new google.maps.LatLng(43.72333292, 12.63552457);
var mapOptions = {
zoom:10,
center: urbino
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var start, end;
var waypts = [new google.maps.LatLng(43.72333292,12.63552457), new google.maps.LatLng(43.72333292,12.63552457), new google.maps.LatLng(43.72333292,12.63552457), new google.maps.LatLng(43.72333292,12.63552457)............];
console.log(waypts.length);
$.each(waypts, function(index){
if(index < waypts.length -1){
var request = {
origin:waypts[index],
destination:waypts[index+1],
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
console.log(index);
console.log(response);
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
console.log('ok');
}
else{
console.log('non ok');
}
});
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
$(document).ready(function(){
calcRoute();
});
EDIT
Came out that supplied points were totally wrong (but that was not my job so i just took them), now with new points directionsService works fine even with just some waypoints.
The problem still remains in part, even if much smaller, cause points supplied from the fusion table are not precisely on the street on all the route. So i still need to figure out how to put pieces together, meaning using the route of the directionsService, styled with the fusion table layer. :)
Used points were totally wrong, now with right set of coordinates, the path supplied by fusion Table is almost perfect, on all the streets long, even though there are some points not really on the streets.
So, what i have to figure out now is how to put fusionTable layer (styled) with directionsService (precise).

Plotting more than 8 waypoints in Google Maps v3

Migrating code from Javascript API 2 to 3. I have a list of locations which i need to plot in the form of a driving directions. This was done in v2 using the following code
directions = new GDirections(map);
directions.loadFromWaypoints(waypoints, {preserveViewport: true});
Here is my attempt at converting this to V3
var request = {
origin: startLoc,
destination: endLoc,
waypoints: waypoints,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
Not the whole code, but the general idea. Seems to work fine, with one little issue. When there are more than 8 waypoints, the call fails. This is expected since Google Maps API v3 Docs states
The maximum allowed waypoints is 8, plus the origin, and destination. Maps API for Business customers are allowed 23 waypoints, plus the origin, and destination. Waypoints are not supported for transit directions.
Since i didn't run in to this issue in v2, is this a new restriction with v3? I wonder if i am using something that was not designed for what i need. This is a very lightly used applicaton with 2 users, so i am not sure if an expensive business license is worth the return. Emails to Google maps team have not yet been returned. Any workarounds/pointers will be of great help. Thanks.
One possible work around (particularly for a lightly used site) is to use multiple DirectionsService requests.
example 1 (addresses)
example 2 (coordinates)
Use Should need to Use array concept like these...
directionsService[i].route({
'origin': start,
'destination': end
'waypoints': waypts,
'travelMode': 'DRIVING'
},
function (directions, status){
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay[j].setDirections(directions);
}
});
in these directionservice and directiondisplay are should be in array logic
and using looping concepts use should need to assign start,end and waypoints dynamically and
try sending multiple request means u ll get the route for n number of latlon's ...but the markers ll be repeat with same name after 8 waypoints for that we remove the default markers by using supressmarkers false property...
As others said, it's imposable to do it using Google's JavaScript API. However, Google does allow up to 23 waypoints with a server-side request. So using PHP and JavaScript, I was able to make a workaround.
What you have to do is get the "waypoint order" from the server side request and then have JavaScript give you directions between each location.
<?php
$startLocation = "40.713,-74.0135";
$endLocation = "40.75773,-73.985708";
$waypoints = array("40.748433,-73.985656|", "40.689167,-74.044444|");
$apiKey = "";
$routeData = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=".$startLoc."&destination=".$endLoc."&waypoints=optimize:true|".$waypoints."&key=".$apiKey));
echo "var waypointsOrder = ". json_encode($routeData->routes[0]->waypoint_order) . ";\n";
?>
var startLocation = {lat: 40.713, lng: -74.0135};
var endLocation = {lat: 40.75773, lng: -73.985708};
//get directions from the origin to the first waypoint
var request = {
origin: startLocation,
destination: JSON.parse(places[waypointsOrder[0]][1]),
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
renderDirections(result, map);
}
});
//get directions from the last waypoint to the destination
var request = {
origin: JSON.parse(places[waypointsOrder[waypointsOrder.length-1]][1]),
destination: endLocation,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
renderDirections(result, map);
}
});
//get directions between each waypoint
for (i = 1; i < waypointsOrder.length; i++) {
var request = {
origin: JSON.parse(places[waypointsOrder[i-1]][1]),
destination: JSON.parse(places[waypointsOrder[i]][1]),
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
renderDirections(result, map);
}
});
}
function renderDirections(result, map) {
var directionsDisplay = new google.maps.DirectionsRenderer ({
map: map
});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(result);
}

More than 2 Origin and destination on Google Maps API 3

I had this example :
https://google-developers.appspot.com/maps/documentation/javascript/examples/directions-panel
and I was wondering how to set routes by 2 or more waypoint? As you know on that application you can get the waypoint: first, set the origin / start point and then set the destination point and the maps will generating the routes. Script :
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);
}
});
}
see : origin : start, destination: end.
Now the Question is : Is that possible to me to set the waypoint by myself? Using LatLong from point-to-point or maybe I just say what the street name that will be the point?
Understand? Sorry I am not good at words but I always wait for the answers, thank you!
Yes it is possible to add waypoints to a directions request:
Here is the description of the waypoint object from the documentation:
https://developers.google.com/maps/documentation/javascript/reference#DirectionsWaypoint
Here is the request description in the documentation (see waypoints):
https://developers.google.com/maps/documentation/javascript/reference#DirectionsRequest
Example with an "address" waypoint
Example with a waypoint specified by coordinates

Sencha directions zoom

All,
When I use the Directions-service from Google Maps, it seems like the map always goes to zoom level 0 (I get an overview of the whole map).
When I set the code for the directions in comment, the zoom level is working well.
This is my code:
/**
* Update route
*/
updateRoute: function ()
{
// route
var start = "Belgium";
var end = "France";
var request =
{
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
directionsDisplay.setMap(this.items.items[0].map);
directionsService.route(request, function(response, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections(response);
}
});
}
This is what I get:
Hopefully someone can help me with this?
Thanks in advance!
The map automatically zooms out to the range required to display the entire route.... I tried ur code.... If you select the start and origin as two places closer (preferably the same area/city) then the zoom level goes down to like 15-16 depending on the distance....
Since you've basically selected Belgium and France (both of which are really broad areas and not a single map point (so to speak) the zoom level goes down to 1 :)
Hope this helps