I am using google map in xamarin android project.
I want the duration timing between two latlng values without using the addresses of those two points.
Can anyone please help me to resolve this problem?
Thanks in Advance.
Use the google api to pass the lat and lng for both start point and destination, this will give you the addresses as well as a distance and duration object.
Using the API with the following example:
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592key=YOUR_API_KEY
you get something like:
{
"destination_addresses" : [ "67-89 Pacific St, Brooklyn, NY 11201, USA" ],
"origin_addresses" : [ "566 Vermont St, Brooklyn, NY 11207, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "6.5 mi",
"value" : 10422
},
"duration" : {
"text" : "34 mins",
"value" : 2037
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
Related
When i try to calculate the distance between Paris and an other city in France, Google API returns a random result. It returns the right distance or ZERO_RESULTS
I use Postman application. I create GET request :
https://maps.googleapis.com/maps/api/distancematrix/json?units=METRIC&mode=WALKING&origins=BRY SUR MARNE , VAL-DE-MARNE , FR&destinations=PARIS , PARIS , FR&key=MY_KEY
API Google returns :
{
"destination_addresses": [
"4100 Lake Dr SE Suite 300, Grand Rapids, MI 49546, USA"
],
"origin_addresses": [
"94360 Bry-sur-Marne, France"
],
"rows": [
{
"elements": [
{
"status": "ZERO_RESULTS"
}
]
}
],
"status": "OK"
}
This is an intended behavior because the destination address is in the USA while the origin is in France which doesn't have any available routes towards each other, If the destination address is not the intended address, I'd suggest that you supply the well-formatted address to avoid this kind of errors.
I am using mapdist function from the ggmap package in R and I have a problem with results (more specifically by using the ReadLines function).
I give 2 addresses to the function, and then it connects to the Google API to return the distance between the 2 addresses.
I give an example below without calling my real addresses.
The result is empty (Status: "Not_found") with R.
$destination_addresses
[1] "Adresse numero 1, France"
$origin_addresses
[1] ""
$rows
$rows[[1]]
$rows[[1]]$elements
$rows[[1]]$elements[[1]]
$rows[[1]]$elements[[1]]$status
[1] "NOT_FOUND"
$status
[1] "OK"
Given that I was sure of my address, I copied the URL (to connect on Google API) on my browser (I tried on Mozilla, Google Chrome and Internet explorer), and I was surprised to obtain a non-empty result.
{
"destination_addresses" : [ "Adresse numero 1, France" ],
"origin_addresses" : [ "Adresse2, France" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "21,0 km",
"value" : 21045
},
"duration" : {
"text" : "17 minutes",
"value" : 1003
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
If I had only one address, the solution would be obvious. My problem is I have approximately 100 addresses with empty results and I can not check all of them.
How could you explain that the return result of ReadLines is not the same that the results on my webpage ?
Thank you for helping me.
The troubles I usually find with ggmap + R is the API does not understand the format.
I always fix this trouble checking the way google writes the address while using maps.googles.com and afterwards I make it reproductible.
I could try by my own if I could check it with real data, although you did not write it.
On the other hand you can subset all the "rows" google didn't give you the data back and try again.
My doc:
"_id" : "bf63XML4bo6CrfP9A",
"comments" : [
{
"user" : "fzkhiAArD4mgAAjbL",
"comment" : "what the hell are you doing in there?",
"commentedAt" : 1422367883366
},
{
"user" : "fzkhiAArD4mgAAjbL",
"comment" : "tada tada",
"commentedAt" : 1422368379037
},
{
"user" : "fzkhiAArD4mgAAjbL",
"comment" : "la la la la la",
"commentedAt" : 1422368393536
},
{
"user" : "fzkhiAArD4mgAAjbL",
"comment" : "no no no",
"commentedAt" : 1422368409692
}
],
"feed_id" : "ym8To4cdwwTcSczkS",
I want to send the recent 3 comments from this doc, how to do it?
I tried different things, nothing working
mycoll.find({_id:res._id},{fields:{sort: {commentedAt: 1}},fields: {comments:{$slice: 3}}});
FYO
I want this in publish funciton.
As far as I'm aware, you cannot sort the comments and then slice them, only slice them. If you really need to sort and slice, you should probably use a transform, although I think this would be computationally more expensive.
However, if you've $pushed comments onto the array chronologically you should be able to do (on the server only):
mycoll.find({_id:res._id},{fields: {comments:{$slice: -3}}});
to get the last 3.
Say you have exposed an API Product in Apigee. You would like to get a list of all the developers and their apps that have registered for the API Product.
There is a call to return those, documented here:
http://apigee.com/docs/api/get-list-keys-apps-developers-or-companies-api-product
But the IDs it returns appear to be useless. If you try developers, for instance:
https://api.enterprise.apigee.com/v1/organizations/YOUR-ORG/apiproducts/YOUR-PRODUCT?query=list&entity=developers
you get back a list of IDs. But to find which developer a given ID relates to is impossible, as the call to get a developer:
http://apigee.com/docs/api/get-developer
only accepts an email address.
How can I get a list of all the developers and their apps registered for a given API product?
The Apps a developer has is nested in each developer:
https://api.enterprise.apigee.com/v1/o/{your org}/developers
will return a list of developers like this:
["email#domain.com", "email2#domain2.com"]
Then you have to loop through each developer to get a list of their apps:
https://api.enterprise.apigee.com/v1/o/{your org}/developers/tesla#weathersample.com
Which gives you a bunch of meta data including apps:
{
"apps" : [ "weather" ],
"companies" : [ ],
"email" : "tesla#weathersample.com",
"developerId" : "Hk5mmLw9kKIM95qF",
"firstName" : "Nikolai",
"lastName" : "Tesla",
"userName" : "Nikolai",
"organizationName" : "jokeindex",
"status" : "active",
"attributes" : [ ],
"createdAt" : 1357858239543,
"createdBy" : "noreply_admin#apigee.com",
"lastModifiedAt" : 1357858239543,
"lastModifiedBy" : "noreply_admin#apigee.com"
}
Finally, if you look at each app you can see the products associated with that developer app:
https://api.enterprise.apigee.com/v1/o/{your org}/developers/tesla#weathersample.com/apps/weather
Gets you this detail:
{
"accessType" : "read",
"appFamily" : "default",
"appId" : "030fdcea-cf97-40b1-96df-12084aea513c",
"attributes" : [ {
"name" : "Developer",
"value" : "tesla#weathersample.com"
}, {
"name" : "DisplayName",
"value" : "Weather"
}, {
"name" : "Notes",
"value" : "not yet"
}, {
"name" : "lastModifier",
"value" : ""
} ],
"callbackUrl" : "http://example.com/callback",
"createdAt" : 1363578857830,
"createdBy" : "adminui#apigee.com",
"credentials" : [ {
"apiProducts" : [ {
"apiproduct" : "weather",
"status" : "approved"
} ],
"attributes" : [ ],
"consumerKey" : "{key}",
"consumerSecret" : "{key}",
"expiresAt" : -1,
"scopes" : [ ],
"status" : "approved"
} ],
"developerId" : "Hk5mmLw9kKIM95qF",
"lastModifiedAt" : 1386042817268,
"lastModifiedBy" : "michael.bissell#apigee.com",
"name" : "weather",
"scopes" : [ ],
"status" : "approved"
}
Take a look at the Org Snapshot Tool on git if you want to interrogate the entire org with one script:
https://github.com/apigee/api-platform-samples/tree/master/tools
This will interrogate every developer and every app and put it into a nice tree structure in your file system for future reference.
I have Postcode in my large database, which contains values like SL5 9JH, LU1 3TQ etc.
Now when I am pasting above postcode to maps.google.com it's pointing to a perfect location..
My requirement is like I want to pass post codes to maps.google.com and it should return a related latitude and longitude of that pointed location, that I want to store in my database.
So, most probably there should be some javascript for that... If anybody have another idea regarding that please provide it..
Thanks in advance...
A quick note for those finding this SO answer. The answer by Daniel Vassallo uses the Google Geocoding API V2 which has now been deprecated. The new v3 API uses a request format like this:
http://maps.googleapis.com/maps/api/geocode/output?parameters
An example for a postcode lookup, returning the data in JSON format is:
http://maps.googleapis.com/maps/api/geocode/json?address=SL59JH,+UK&sensor=false
This returns a JSON array that includes the lat and long in results->geometry->location->lat and results->geometry->location->lng
Example response:
{
"results" : [
{
"address_components" : [
{
"long_name" : "SL5 9JH",
"short_name" : "SL5 9JH",
"types" : [ "postal_code" ]
},
{
"long_name" : "Windsor and Maidenhead",
"short_name" : "Windsor and Maidenhead",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "United Kingdom",
"short_name" : "GB",
"types" : [ "country", "political" ]
},
{
"long_name" : "Ascot",
"short_name" : "Ascot",
"types" : [ "postal_town" ]
}
],
"formatted_address" : "Ascot, Windsor and Maidenhead SL5 9JH, UK",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.39655490000001,
"lng" : -0.66024660
},
"southwest" : {
"lat" : 51.39457330,
"lng" : -0.6624574999999999
}
},
"location" : {
"lat" : 51.39539040,
"lng" : -0.66096740
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.39691308029150,
"lng" : -0.6600030697084980
},
"southwest" : {
"lat" : 51.39421511970851,
"lng" : -0.6627010302915021
}
}
},
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
The API spec is available here: https://developers.google.com/maps/documentation/geocoding/
The technical term for the process you describe is called reverse geocoding. Google offers the The Google Geocoding Web Service New working Google Geocoding Link, where you can do reverse geocoding on the server side, instead of in JavaScript on the client-side.
For example, if you try the following URLs in your browser, you would get back the latitude and longitude of the postcode passed in the q parameter, in CSV format:
http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false
http://maps.google.com/maps/geo?q=LU13TQ,+UK&output=csv&sensor=false
This is how you would be able to reverse geocode your postcodes in php, for example:
$url = 'http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false';
$data = #file_get_contents($url);
$result = explode(",", $data);
echo $result[0]; // status code
echo $result[1]; // accuracy
echo $result[2]; // latitude
echo $result[3]; // longitude
Note that as Pekka suggested in another answer, the Google Maps API Terms of Use seem to prohibit the storage of the results, unless the store acts as a cache for data that will used in Google Maps. You may want to get in touch with Google and enquire on the Google Maps API Premier to have more flexible terms of use for your geocoding requirements.
The Ordnance Survey have released the postcode locations on a Creative Commons licence (CC-BY v3, IIRC). It would be a lot less hassle (and a lot clearer legally) to use that instead.
There's even a version with WGS84 (a.k.a. GPS) coordinates mirrored by mySociety
The Google Geocoding API does that, although if I remember correctly, their terms of service forbid local storage of the geocoding results.
I know this is an old question, but just chipping in here with how I managed to achieve the same thing (in PHP, but should be fairly simple):
I had a database of thousands of differently formatted postcodes. I cleaned each and every one of them up, uniformly, with this function and batch updates:
function clean_postcode($postcode)
{
$out = preg_replace("/[^a-zA-Z0-9]/", '',strtoupper($postcode));
if(strlen($out)>3)
{
$out = substr($out, 0, (strlen($out) -3)).' '.substr($out, -3);
}
return $out;
}
Now that all postcodes are formatted uniformly, I downloaded and imported the Ordnance Survey's Code-Point data set (free) - https://www.ordnancesurvey.co.uk/opendatadownload/products.html
I imported all their CSVs into my database in a separate codepoint table. From each CSV I imported the postcode, and the Eastings and Northings values.
I ran the clean_postcode() function again in batch on all the Ordnance Survey data in my DB. About 50% of the postcodes have spaces in, and 50% don't - after this they were uniformly set.
I ran the following PHP script on each and every postcode in the codepoint table and saved the Latitude and Longitude values returned into this table: http://bramp.net/blog/os-easting-northing-to-lat-long
All done! You can now match up and pull a Lat/Lon value based on well-formatted postcodes.
Further reading: http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/
Check out ALgolia places here. Super fast, Open source and MIT