Google Static Maps API - Error while encoding polyline - google-maps

I'm using google maps' Interactive Polyline Encoder Utility to plot locations on a map. When I try to plot 31.63089000, 74.87155200, I get an error "Invalid location entered. Must be in range of -90 to 90".

Seems to be a problem with the form code here:
var lat = document.getElementById('txtLatitude').value;
var pLat = parseFloat(lat);
if (pLat.toString() != lat) {
alert('Invalid latitude entered. Must be in range of -90 to 90');
return;
}
It works for me if I truncate your numbers to 31.63089 and 74.871552. The problem in that javascript is that parseFloat is truncating the trailing zeroes off the end. So it converts 31.63089000 to 31.63089. And then 31.63089 is != what was entered in the form, 31.63089000, hence the error. Suggest you file this as a bug with whoever's responsible.

Related

Getting Latitude and Longitude from Google Places Autocomplete API

Im using the Google Places Autocomplete API, to have an input in which users type a city and get suggestions for which place they are searching for.
I need to get not only the name of the place but the Latitude and Longitud of the place for then centering a google map there.
The problem is that Google Places Autocomplete API just returns description of the place but not the coordinates, at least with what i tried.
Anybody knows if there is a way to get the Latitud & Longitud in the same request?
Many thanks to any help :')
All the information you are looking for can be found inside the Place Result. This is what your code would look like:
var input = document.getElementById("address");
var autocomplete = new google.maps.places.Autocomplete(input, {types: ["geocode"]});
autocomplete.addListener("place_changed", function() {
var placeResult = autocomplete.getPlace();
var name = placeResult.name;
// The selected place's position
var location = placeResult.geometry.location;
// The preferred viewport when displaying this Place on a map.
// This property will be null if the preferred viewport for the Place is not known.
var viewport = placeResult.geometry.viewport;
// This is assuming your google map is saved in a variable called myMap
if (viewport) {
myMap.fitBounds(viewport)
}
else if (location) {
myMap.setCenter(location)
}
});
I recently created a jQuery plugin that makes interacting with Google Maps Autocomplete a breeze. This would be the code to center a map every time a place was selected:
$("#address").geocomplete({
map: myMap
});
The plugin will also allow you to autofill an address form, adds custom styling, provides callbacks and fixes an annoying scrolling issue.

Google Apps Script - How to get driving distance from Maps for two points in spreadsheet

Good evening,
I have a spreadsheet where I record my daily driving mileage when working. The column headings are: Date, Point A, Point B, Point C, Point D, Point E, Point F, Point G, and Trip Mileage.
Currently, I manually use Google Maps to determine driving distance between points. What I'd like to see happen instead is for a script to pull the points data from my spreadsheet, determine distance between the points and total distance of each trip (i.e. row), and insert the total in the last column.
I believe I have this essentially all set up except I can't figure out how to get the distance data from Google Maps. From reading similar question, I sense someone might tell me to refer to the Maps Distance Matrix API. I've referred to this. I'm a beginner programmer, but it appears to me to be oriented toward apps, not Google docs. Is this the case? Or can it be made to serve my project?
Thank you in advance for all of your wonderful advice, comments, suggestions, and encouragement!!!
BTW, here is my code currently:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mileageLogSheet = ss.getSheetByName("Mileage Log");
var dataRange = mileageLogSheet.getDataRange(); //Returns a Range corresponding to the dimensions in which data is present.
var numRows = dataRange.getNumRows();
var numCol = dataRange.getNumColumns(); //numCol = 9.
//Returns the range with the top left cell at the given coordinates (row, column) with the given number of rows and columns.
var rangeToCompute = mileageLogSheet.getRange(10, 2, numRows-9, numCol-1);
var address1,
address2;
for(var rowCoord = 1; rowCoord < numRows-9; rowCoord++){
for(var colCoord = 1; colCoord < 8; colCoord++){
if(rangeToCompute.getCell(rowCoord, colCoord).getValue() != ""){
address1 = rangeToCompute.getCell(rowCoord, colCoord).getValue();
address2 = rangeToCompute.getCell(rowCoord, colCoord+1).getValue();
var directions = Maps.newDirectionFinder()
.setOrigin(address1)
.setDestination(address2)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
//Nope, this doesn't work!:
var distanceValue = directions.routes[0].legs[0].distance.value;
var totalTripDistance;
}
}
}
}
You can take use of the Google Maps WEB API's Direction API. Which give you a detail result on direction from point A to point B, which include the distance information you want.
To get started, you need an API KEY for using the Google Maps Direction API. Go to console.developers.google.com to create an account, create a project, enable the Directions API in your project and then create a credential (API KEY). You should able to create it as server API key and give it the address 0.0.0.0/0, which means all computer on earth, as long as you keep this project to yourself. For more information on creating API KEYS, you can take a look at this section.
Then in the spreadsheets, you need to first create the request URL by CONCAT your key, origin and destination. Here is how I did it:
=CONCAT(CONCAT(CONCAT(CONCAT(CONCAT("https://maps.googleapis.com/maps/api/directions/json?origin=",SUBSTITUTE(B2," ","+")),"&destination="),SUBSTITUTE(C2," ","+")),"&key="),$A$2)
*Note that you need to SUBSTITUTE spaces " " to pluses "+"
Then under the Tools > Script editor..., create an empty script and replace everything with the following code, and save:
function getDistance(url) {
var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json);
var distance = data.routes[0].legs[0].distance.text;
return distance;
}
This is a customized function that parse the Google Maps Direction API JSON result and return the distance information. The JSON usually contains multiple routes and under the first route- routes[0], exist multiple legs and in the first leg- legs[0], exist a distance, which include a text and value field, which are a human readable field, and a integer number that represent the distance in meter.
return to the spreadsheets and you can call =getDistance(CellWithURL) to get the distance between two places.
I created an example here, hope it helps.
**ps. somehow it works without the API KEY too.. but you should apply for one for otherwise you violate the ToS.

Google Places API - Counting the total number of places in a given radius around a lat / long?

Slightly unusual request, but just wondered if anyone knows of a way to calculate the total number of places in the Google Places places database within a given radius (or polygon) around a lat / long?
Many thanks in advance!
There is no way to query the Places API for the total number of places that fall within a given radius in the Google database. When you query the Places API, the maximum number of results that will be returned is 20 (twenty) as described in this question/answers: What is the proper way to use the radius parameter in the Google Places API?.
Maybe I'm misreading the question, but here goes. Check out the Places API example; its search results is an array, so you can read results.length as the number of places found (returned by the request, see below). The coverage radius around a latLng or rectangular bounds is set in the request.
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}

GMaps API: Search Map via Lat/LonCoordinates and Addresses

I've been using the Geocoder service in the Google Maps API to power a search box on my map. I wish to allow users to search freely by address, city and coordinates and perhaps anything else that is supported. Until recently if I passed latitude/longitude coordinates to the geocoder it would simply return me a result of those specific coordinates but lately it has changed to do a reverse lookup and provide me the nearest address to the coordinates. I actually want the location directly at the coordinates as that is what is most relevant.
Any ideas how to either parse out the various input forms of coordinates from the search box or get the geocoder to revert to its earlier behaviour?
I'm not sure why it would have changed to showing reverse geocoding, without seeing the code. However, I would suggest using the Autocomplete feature of the Places API Library instead.
Couldn't you just use a regex to check if the input entered is a lat/lng pair? And then if it is parse that pair and navigate to the coordinates directly. Something like:
var latLngRegex = /^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/; //Regex for checking that it is a latlng pair that has been entered
var address = document.getElementById("txt_googlesearch").value;
if (address=='' || address=='Search') {
return;
}
if (latLngRegex.test(address)) //Run the regex against the entered value
{
var coords = address.split(","); //Split the address into 2 decimal values
var mapPoint = new GLatLng(parseInt(coords[0]), parseInt(coords[1])); //Create a gLatLng from the split values
map.setCenter(mapPoint); //Move the map to the entered location
return;
}
//Call Geocoder as before

Google Maps API - Finding Waypoint That Caused Error

We have been developing an ASP.NET application that works with the Google Maps API to assist in logistics and planning for a small shipping company in our area. One of the features is for the company to input a list of customers and the system will package up all the customer addresses as a series of waypoints, send it off to Google, and get back the direction information for the route.
One problem that we have been having is that many of the addresses in our database are not great and will often times are not able to be processed by Google Maps. When we do this we receive back an error code, but I would like to be able to determine which waypoint was the one to fail (that way the client can then go and fix the address in the database).
EDIT Here is a chunk of the code that handles the initialization and current error handling:
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
gdir = new GDirections(map);
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);
if (document.getElementById("<%=hiddenWayPoints.ClientID %>").getAttribute("value") != '')
{
setDirections();
}
}
}
function setDirections() {
var waypoints = new Array();
var str = document.getElementById("<%=hiddenWayPoints.ClientID%>").getAttribute("value");
waypoints = document.getElementById("<%=hiddenWayPoints.ClientID %>").getAttribute("value").split(":");
gdir.loadFromWaypoints(waypoints, {getSteps:true});
}
function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
else alert("An unknown error occurred.");
}
The problem is that the LoadFromWayPoints() method in the GDirections object doesn't seem to return status based on each individual waypoint, but from just the entire list (if one fails it all fails).
The only real solution I can think of (without performing checks in other areas of the system) is to send each waypoint off to Google Maps in a separate request and check it's validity that way prior to sending off the entire waypoints list for the GDirections object, but that seems incredibly inefficient (especially when dealing with a larger set of customer locations).
If you are using the GClientGeocoder object to do your requests, then you will get back an appropriate response code for each getLocations call:'
function logAddress (response)
{
if (!response || response.Status.code != 200)
{
// log the response.name and the response.Status.code
return;
}
// otherwise everything was fine
}
for (var i = 0; i < addresses.length; i++)
{
var geocoder = new GClientGeocoder ();
geocoder.getLocations (addresses[i], logAddress);
}
Their are various response codes, but I am guessing you want to inform the user when you get a 602 - Unknown Address.
EDIT:
Yep, you will only get a single error callback for loadFromWaypoints for the entire directions request. What you are doing is more than just a simple geocoding request, you are actually generating directions and rendering overlays to a map for a sequence of addresses values. I suggest a couple of solutions:
You could do a getLocation request before your loadFromWaypoints request (as you suggested) and then use the latitude,longitude data returned for each address as the parameter for your loadFromWayPoints. This splits the geocoding processing out of the loadFromWayPoints request, but adds the extra round trip for each geocoding lookup. This is really something you only want to do once, when the user first enters the address (see next).
When the user enters the address information for the first time you can do a GClientGeocoder getLocations lookup at the time and get the latitude,longitude to store in your database along with the address. That way, if the user enters an address that can't be geocoded, then you can ask them to re-enter the address, or perhaps let them select the location on a google map (and get the lat,lng from that). This doesn't solve the problems with address data you have now, but perhaps you can write some code to run through you existing data (offline) and flag the addresses in the db that are not able to be geocoded.