I'm making a navigation application, I have a bunch of routes being returned from Googles Directions API (set alternatives to true), which the user will then choose from. What I'd like to do is launch the Map intent for Google Maps with the selected route passed in.
I'm wondering if this is possible?
I've seen examples of passing in saddr and daddr which will automatically show the best route, but instead I'd like it to display the one selected by a user from one of the direction results which may not the best (most optimum).
Thanks
After reading the google direction api, I saw that in its response type (https://developers.google.com/maps/documentation/directions/intro#sample-response), we have for every step, latitude and longitude defined.
Say your output route looks like (this is dummy, not original):
{
...
"routes": [ {
"summary": "I-40 W",
// There are many legs, but say user choses this below first leg.
"legs": [ {
"steps": [
{
"travel_mode": "DRIVING",
"start_location": {
"lat": 41.8507300,
"lng": -87.6512600
},
"end_location": {
"lat": 41.8525800,
"lng": -87.6514100
},
"polyline": {
"points": "a~l~Fjk~uOwHJy#P"
},
"duration": {
"value": 19,
"text": "1 min"
},
"html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
"distance": {
"value": 207,
"text": "0.1 mi"
}
},
{
"travel_mode": "DRIVING",
"start_location": {
"lat": 41.8107300,
"lng": -87.6012600
},
"end_location": {
"lat": 41.8725800,
"lng": -87.6414100
},
"polyline": {
"points": "a~l~Fjk~uOwHJy#P"
},
"duration": {
"value": 19,
"text": "1 min"
},
"html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
"distance": {
"value": 207,
"text": "0.1 mi"
}
}
"start_address": "Oklahoma City, OK, USA",
"end_address": "Los Angeles, CA, USA"
} ],
.... // many other legs ....
"legs":[ { ..... } ]
.
.
"copyrights": "Map data ©2010 Google, Sanborn",
...
..
.
} ]
}
]
}
Now, above for each step, we have lat and lng entries. We will use those to create our intent request.
Our request will become :
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://maps.google.ch/maps?saddr=Oklahoma City, OK, USA&daddr=Los Angeles, CA, USA to:41.8525800,-87.6514100 to: 41.8107300,-87.6012600"));
startActivity(intent);
Notice that I have used the steps lat and lng along with to: prefix in the url. Use can use only upto two to: prefixes in this url, because google maps app doesn't acknowledge all the waypoints but only the first and the last, when passed as intent.
To put it generalized way :
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("https://maps.google.ch/maps?saddr=[address1]&daddr=[address2] to:[address3] to: [address4]"));
startActivity(intent);
So, this will open google maps app with navigation marked from [address1] to [address2] passing through [address3] and [address4].
Hope this solves problem.
Related
I am using the Directions API from Google. Now I would like to determine the current step to the current location of a user.
Here is an example from the google website:
{
"status": "OK",
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJ7cv00DwsDogRAMDACa2m4K8",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ69Pk6jdlyIcRDqM1KDY3Fpg",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJgdL4flSKrYcRnTpP0XQSojM",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJE9on3F3HwoAR9AhGJW_fL-I",
"types" : [ "locality", "political" ]
}
],
"routes": [ {
"summary": "I-40 W",
"legs": [ {
"steps": [ {
"travel_mode": "DRIVING",
"start_location": {
"lat": 41.8507300,
"lng": -87.6512600
},
"end_location": {
"lat": 41.8525800,
"lng": -87.6514100
},
"polyline": {
"points": "a~l~Fjk~uOwHJy#P"
},
"duration": {
"value": 19,
"text": "1 min"
},
"html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS
Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
"distance": {
"value": 207,
"text": "0.1 mi"
}
},
...
... additional steps of this leg
...
... additional legs of this route
"duration": {
"value": 74384,
"text": "20 hours 40 mins"
},
"distance": {
"value": 2137146,
"text": "1,328 mi"
},
"start_location": {
"lat": 35.4675602,
"lng": -97.5164276
},
"end_location": {
"lat": 34.0522342,
"lng": -118.2436849
},
"start_address": "Oklahoma City, OK, USA",
"end_address": "Los Angeles, CA, USA"
} ],
"copyrights": "Map data ©2010 Google, Sanborn",
"overview_polyline": {
"points": "a~l~Fjk~uOnzh#vlbBtc~#tsE`vnApw{A`dw#~w\\|tNtqf#l{Yd_Fblh#rxo#b}#xxSfytA
blk#xxaBeJxlcBb~t#zbh#jc|Bx}C`rv#rw|#rlhA~dVzeo#vrSnc}Axf]fjz#xfFbw~#dz{A~d{A|zOxbrBbdUvpo#`
cFp~xBc`Hk#nurDznmFfwMbwz#bbl#lq~#loPpxq#bw_#v|{CbtY~jGqeMb{iF|n\\~mbDzeVh_Wr|Efc\\x`Ij{kE}mAb
~uF{cNd}xBjp]fulBiwJpgg#|kHntyArpb#bijCk_Kv~eGyqTj_|#`uV`k|DcsNdwxAott#r}q#_gc#nu`CnvHx`k#dse
#j|p#zpiAp|gEicy#`omFvaErfo#igQxnlApqGze~AsyRzrjAb__#ftyB}pIlo_BflmA~yQftNboWzoAlzp#mz`#|}_
#fda#jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC"
},
"warnings": [ ],
"waypoint_order": [ 0, 1 ],
"bounds": {
"southwest": {
"lat": 34.0523600,
"lng": -118.2435600
},
"northeast": {
"lat": 41.8781100,
"lng": -87.6297900
}
}
} ]
}
I thought I could use the start_location field and determine the distance between the current location and the start_location fields but I guess that is wrong.
Has anybody a working solution for this?
Any help will be appreciated.
You need to decode "points" tag from "polyline" section (from documentation:
polyline contains a single points object that holds an encoded
polyline representation of the step. This polyline is an approximate
(smoothed) path of the step.
more information about polyline encoding in Google Maps you can find e.g. here) of each step and leg from "legs" array:
"legs": [ {
"steps": [ {
...
"polyline": {
"points": "a~l~Fjk~uOwHJy#P" <--- need to decode this string
},
...
} ]
}]
for example via PolyUtil.decode() of Maps SDK for Android Utility Library and you got a sequence of LatLngs for steps.
Than you need to create loop via all steps and find step that contains point with current location coordinates via PolyUtil.isLocationOnPath() call. Something like that:
JSONObject jsonObject = new JSONObject(jsonStringBuilder.toString());
JSONArray legsArr = jsonObject.getJSONArray("legs");
// for each leg
for (int i = 0; i < legsArr.length(); i++) {
// get array of steps
JSONArray stepsArr = legsArr.getJSONObject(i).getJSONArray("steps");
// for each step
for (int j = 0; j < stepsArr.length(); j++) {
// get step
JSONObject step = stepsArr.getJSONObject(j);
JSONObject polyline = step.getJSONObject("polyline");
String encodedPoints = polyline.getString("points");
// decode encoded path to list of points LatLng
List<LatLng> decodedPoints = PolyUtil.decode(encodedPoints);
if (PolyUtil.isLocationOnPath(currentLocation, decodedPoints, true, 100)) {
// your currentLocation is on the step of route
// constant 100 - is tolerance in meters
}
}
}
If you need to determine exactly segment for currentLocation, not entire step you need to add loop via all segments in step:
...
// decode encoded path to list of points LatLng
List<LatLng> decodedPoints = PolyUtil.decode(encodedPoints);
List<LatLng> segment = new ArrayList(2);
for (int k = 0; k < decodedPoints.size() - 1; k++) {
segment.clear();
segment.add(decodedPoints.get(k));
segment.add(decodedPoints.get(k+1));
if (PolyUtil.isLocationOnPath(currentLocation, segment, true, 100)) {
// your currentLocation is on the segment of step
}
}
NB! It's just approach description, not fully functional code.
I have recently implemented Google Custom search. I am getting a result but it every link in it whether it is for website or image or thumbnail, it is from maps.
example result -
{
"kind": "customsearch#result",
"title": "Saga Tree",
"htmlTitle": "Saga \u003cb\u003eTree\u003c/b\u003e",
"link": "https://***www.google.com/mymaps***/viewer?mid=1yF1gMCZhBaeEouT9GB-7bQqXmuw&hl=en",
"formattedUrl": "***https://www.google.com/mymaps***/viewer?mid...7bQqXmuw&hl...",
"pagemap": {
"cse_thumbnail": [
{
"width": "221",
"height": "228",
"src": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR2SZoZ2AF7PUVw5wGucLb5Hd4k1Bwkt3x6__ed8E_01Mlq5Fwd7nKpKI2b"
}
],
"website": [
{
"name": "Saga Tree",
"description": "Saga Tree",
"url": "***https://www.google.com/maps/d/viewer***?mid=1yF1gMCZhBaeEouT9GB-7bQqXmuw&hl=en",
"image": "***https://www.google.com/maps***/d/thumbnail?mid=1yF1gMCZhBaeEouT9GB-7bQqXmuw&hl=en"
}
],
"metatags": [
{
"viewport": "initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0,width=device-width",
"og:type": "website",
"og:title": "Saga Tree",
"og:description": "Saga Tree",
"og:url": "***https://www.google.com/maps***/d/viewer?mid=1yF1gMCZhBaeEouT9GB-7bQqXmuw&hl=en",
"og:image": "***https://www.google.com/maps***/d/thumbnail?mid=1yF1gMCZhBaeEouT9GB-7bQqXmuw&hl=en",
"twitter:card": "summary_large_image",
"twitter:title": "Saga Tree",
"twitter:description": "Saga Tree",
"twitter:image:src": "https://www.google.com/maps/d/thumbnail?mid=1yF1gMCZhBaeEouT9GB-7bQqXmuw&hl=en"
}
],
"cse_image": [
{
"src": "https://www.google.com/maps/d/thumbnail?mid=1yF1gMCZhBaeEouT9GB-7bQqXmuw&hl=en"
}
]
}
}
This is with every result. I want results as if I have searched it in the browser.
This usually happens when the Custom Search API performs search only with the default included site ( www.google.com ). This is the default. To change it go to Basics -> Sites to Search under your Custom Search engine and choose "Search the entire web but emphasise included sites" instead of the default "Search only included sites".
Also ensure Image Search is enabled under Basics.
My JSON contains only name of the food categories, Looks like below
[
{
"cu_id": "2",
"cuisine_type": "American"
},
{
"cu_id": "24",
"cuisine_type": "Sandwich"
},
{
"cu_id": "17",
"cuisine_type": "Seafood"
},
{
"cu_id": "29",
"cuisine_type": "Sports Bar"
},
{
"cu_id": "5",
"cuisine_type": "Steak"
},
{
"cu_id": "20",
"cuisine_type": "Sushi"
}
]
I want to get default image automatically from Internet by searching 'cuisine_type', How to do that?
You can use pixabay api it's a free RESTful api for searching and retrieving free images and videos released on Pixabay under Creative Commons CC0.
Sending a GET request like this :
https://pixabay.com/api/?key={ KEY }&q=query&image_type=photo
where :
Key : the api key you get after creating an account
q : the queried object you need to look for
image_type : what kind of data you need (picture, video)
returned data will be something like this :
{
"total": 4692,
"totalHits": 500,
"hits": [
{
"id": 195893,
"pageURL": "https://pixabay.com/en/blossom-bloom-flower-yellow-close-195893/",
"type": "photo",
"tags": "blossom, bloom, flower",
"previewURL": "https://static.pixabay.com/photo/2013/10/15/09/12/flower-195893_150.jpg"
"previewWidth": 150,
"previewHeight": 84,
"webformatURL": "https://pixabay.com/get/35bbf209db8dc9f2fa36746403097ae226b796b9e13e39d2_640.jpg",
"webformatWidth": 640,
"webformatHeight": 360,
"imageWidth": 4000,
"imageHeight": 2250,
"imageSize": 4731420,
"views": 7671,
"downloads": 6439,
"favorites": 1,
"likes": 5,
"comments": 2,
"user_id": 48777,
"user": "Josch13",
"userImageURL": "https://static.pixabay.com/user/2013/11/05/02-10-23-764_250x250.jpg",
},
{
"id": 14724,
...
},
...
]
}
for more detail visit this link
I'm developing a iOS, Android and Windows Phone app, all in native language. Atm I'm calculating distance between current location and a list of points. Here's a sample on how i do this on Android:
Location currentLocation = new Location( "CurrentLocation" );
Location.distanceBetween(mLatitude, mLongitude, tempLoc.getLatitude(), tempLoc.getLongitude(), results);
Problem is that Location.distanceBetween gives me a distance in birds eyes, i would like to change this to give me the distance for driving directions(fastest route)(only need the distance, not drawing it out on a map). I'm not even sure if this is possible. So i got 2 questions.
Is this even possible with google API?
If yes, what options do i have(keep in mind that developing for 3 platforms)?
Thanks in advance.
You could ask Google API for the driving route between those two points, and iterate over each step, which includes the distance.
https://developers.google.com/maps/documentation/directions/#JSON
For example, you could do a request like this:
http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false
but using coordinates instead of addresses. The result would be something like this:
{
"status": "OK",
"routes": [ {
"summary": "I-40 W",
"legs": [ {
"steps": [ {
"travel_mode": "DRIVING",
"start_location": {
"lat": 41.8507300,
"lng": -87.6512600
},
"end_location": {
"lat": 41.8525800,
"lng": -87.6514100
},
"polyline": {
"points": "a~l~Fjk~uOwHJy#P"
},
"duration": {
"value": 19,
"text": "1 min"
},
"html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
"distance": {
"value": 207,
"text": "0.1 mi"
}
},...
Maybe you could add the distance value of each step: 207 + ...
I have an address field in my app where the user needs to enter the required address. I have used google Geocoder to get the GPS coordinates of the address . But now I want to make it easier for the user by using Places Autocomplete . But Places Autocomplete only returns the address , Id and reference of the place .
Is there a way to get the GPS coordinates of the address selected by the user using Places Autocomplete API ? Do I have to use Geocoder again after the user selects his address from Places Autocomplete ?
Or Should I use Places API again after the user selects his address to get the GPS coordinates of the address ? I dont want to send multiple requests to the Places API because of the usage limits in place . . This is the expected response from Places API ..There is no GPS coordinates in the response .
"status": "OK",
"predictions": [ {
"description": "Paris, France",
"id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
"reference": "CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc",
"terms": [ {
"value": "Paris",
"offset": 0
}, {
"value": "France",
"offset": 7
} ],
"types": [ "geocode" ],
"matched_substrings": [ {
"offset": 0,
"length": 5
} ]
}, {
"description": "Paris, TX, United States",
"id" : "518e47f3d7f39277eb3bc895cb84419c2b43b5ac",
"reference": "CjQjAAAAHnbxZZ...BDR3iIOFdMTxwo1jHg",
"terms": [ {
"value": "Paris",
"offset": 0
}, {
"value": "TX",
"offset": 7
}, {
"value": "United States",
"offset": 11
} ],
"types": [ "geocode" ],
"matched_substrings": [ {
"offset": 0,
"length": 5
} ]
}, {
"description": "Paris, Ontario, Canada",
"id" : "e7ac9c89d4a590305242b0cb5bf43064027223c9",
"reference": "CjQhAAAAIv_YWYt...F8KZHY36TwMrbyu_g",
"terms": [ {
"value": "Paris",
"offset": 0
}, {
"value": "Ontario",
"offset": 7
}, {
"value": "Canada",
"offset": 16
} ],
"types": [ "geocode" ],
"matched_substrings": [ {
"offset": 0,
"length": 5
} ]
}
You simply use the Place Details part of the Places API where you get the actual place from the reference in each suggestion using the "reference" value. For example:
https://maps.googleapis.com/maps/api/place/details/json?reference=CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc&sensor=true&key=AddYourOwnKeyHere
This will give you information about the place including its coordinates (point for a specific location, bounds for an area).