Finding driving directions using google in iOS, Android and Windows Phone app - android

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 + ...

Related

GeoJSON properties - Kotlin

i have some response form HTTP API - GeoJSON data, but how i can get only some values from features --> properties in Kotlin - Android Studio? Or is there some statement where I can determine for example all object where "year==2014" to array/list? So i want only some properties to array/list or variable. Thank you for your help :)
This working, but how to loop for each properties?
val test = jsonObject.getJSONArray("features").getJSONObject(0).getJSONObject("properties").getString("year")
{
"type": "FeatureCollection",
"name": "cycl_accidents_cycl_neh_xytabletopoint_spa1",
"crs": { "crs".
"type": "name",
"properties": { "properties".
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "feature",
"properties": {
"objectid": 1,
"join_count": 1,
"target_fid": 1,
"id": 60040140073.0,
"d": -604774.832,
"e": -1162719.75,
"datum": "2014-08-03T00:00:00Z",
"day": 7,
"year": 2014,
"month": 8,
"collision": "other type of accident",
"cause": "driving on the wrong side of the road, driving into oncoming traffic",
"alcohol": "Yes, blood alcohol content between 0,8‰ and 1,0‰",
"at fault": "driver of a non-motor vehicle",
'consequences': 'accident resulting in death or injury',
'road condition': 'surface dry, not contaminated',
'weather conditions': 'not obstructed',
'visibility': 'at night - no public lighting, visibility not impaired by weather conditions',
'visibility': 'good',
'mishap': 'none or none of the above',
"type_of_community": "road monitored (in selected cities)",
'type of vehicle': 'bicycle',
'rider_status': 'under the influence of alcohol, blood alcohol content up to 0,99 ‰',
"affected_ride": 1,
"person": "driver",
"mark_person": "without helmet (only for motorcyclists or cyclists)",
"gender": "male",
"consequence": "slight injury",
"killed_os": 0,
"severely_injured_os": 0,
"slightly_injured_os": 1,
"material damage": 0,
"time": 430,
"hour": 4,
"month_t": "august",
"day of the week": "Sunday",
"age_group": "19-24",
"name": "Brno-Bosonohy",
"point_x": 16.52074206,
"point_y": 49.17113043000001,
"globalid": "{CD1B3254-7DD0-478E-8B3A-C6221ADC3DAB}"
},
"geometry": {
"type": "point",
"coordinates": [
16.520742066000025,
49.171130437000045
]
}
},
{
"type": "Feature",
"properties": {
"objectid": 2,
"join_count": 1,
"target_fid": 2,
"id": 60206100089.0,
"d": -596241.506,
"e": -1157759.125,
"datum": "2010-01-21T00:00:00Z",
"day": 4,
"year": 2010,
"month": 1,
"collision": "collision with a moving non-rail vehicle",
"cause": "driver not fully engaged in driving the vehicle",
"alcohol": "No",
"at fault": "by the driver of the motor vehicle",
'consequences': 'accident resulting in death or injury',
'road condition': 'surface dry, dirty (sand, dust, leaves, gravel, etc.)',
'weather conditions': 'not obstructed',
'visibility': 'at night - with public lighting, visibility not impaired by weather conditions',
'visibility': 'good',
'accident location': 'near a pedestrian crossing (within 20 m)',
'road type': 'local road',
'type of vehicle': 'bicycle',
'condition_ridic': 'good - no adverse circumstances detected',
"affected_ridice": 1,
"person": "driver",
"person_mark": "with helmet (only for motorcyclists or cyclists)",
"gender": "male",
"consequence": "slight injury",
"killed_os": 0,
"severely_injured_os": 0,
"slightly_injured_os": 1,
"material damage": 20,
"time": 1829,
"hour": 18,
"month_t": "January",
"day of the week": "Thursday",
"age_group": "33-44",
"name": "Brno-sever",
"point_x": 16.62980964,
"point_y": 49.22375737,
"globalid": "{83269995-7595-4174-B258-92B2231CF8F2}"
},
"geometry": {
"type": "point",
"coordinates": [
16.629809646000069,
49.223757371000033
]
}
},
]
}
I am assuming features is the list which you get after parsing your response.
first parse your response to your model class. please check this link
for that.
https://stackoverflow.com/a/68501606/7248394.
for example
val apiData = Gson().fromJson(readJson, YourModelClass::class.java)
val features = apiData.features
val featureList = features.filter{
it.properties.year =="2014"
}
so this will give featureList of 2014 year.

Android Maps Coordinates

I am getting json response from an Api. The response looks like this.
{
"CarsList": [{
"coordinate": {
"latitude": 53.46036882190762,
"longitude": 9.909716434648558
},
"heading": 344.19529122029735
},
{
"coordinate": {
"latitude": 53.668806556867445,
"longitude": 10.019908942943804
},
"heading": 245.2005654202569
},
{
"coordinate": {
"latitude": 53.58500747958201,
"longitude": 9.807045083858156
},
"heading": 71.63840043828377
}
]
}
I need to create custom list items using the json Response data.
I am using latitude and longitude to get the location. I am confused as to what the "heading" parameter represents. Please help me in figuring out the purpose of this "heading" parameter. Thanking in advance.
Here is a definition of heading in the google documentation:
When navigating on a sphere, a heading is the angle of a direction from a fixed reference point, usually true north. Within the Google Maps API, a heading is defined in degrees from true north, where headings are measured clockwise from true north (0 degrees).
Also you can see how it is calculated visually:
https://developers.google.com/maps/documentation/javascript/examples/geometry-headings

Launch Android Map intent with specific direction overlay from directions API result

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.

Request buildings around and area osm android

I would like to perform a request to OSM Overpass Turbo API or Overpass API from and Android Application so i can get the buildings around and area using JSON.
Something similar to
http://overpass-turbo.eu/ with the query presented below:
[out:json][timeout:25];
// gather results
(
// query part for: “building”
way["building"](37.98350674557998,23.72600823640823,37.98552989685638,23.728837966918945);
relation["building"](37.98350674557998,23.72600823640823,37.98552989685638,23.728837966918945);
);
// print results
out body;
>;
out skel qt;
The thing is that i need the results in geojson like the result below but i cannot find any query that gives me the below result.
{
"type": "FeatureCollection",
"generator": "overpass-turbo",
"copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.",
"timestamp": "2015-03-23T20:41:02Z",
"features": [
{
"type": "Feature",
"id": "relation/2604192",
"properties": {
"#id": "relation/2604192",
"building": "yes",
"type": "multipolygon"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
23.7319885,
37.9752441
],
[
23.7319787,
37.9751612
],
[
23.7319494,
37.9751641
],
[
23.7319372,
37.9748376
],
[
23.7319702,
37.9748318
],
]
]
}
},
I get only results with nodes but not with geometry.
{
"type": "way",
"id": 25107859,
"nodes": [
2373953582,
2373953586,
2373953592,
2373953599,
2373953597,
2373953636,
2373953633,
2373953626,
273319309,
2373953582
],
"tags": {
"building": "yes"
}
},
After some useful comments (thanks scai) i found out that overpass turbo is not queried automatically. Therefore i searched for overpass api and i found out this site with a query for buildings to overpass api exporting to GeoJSON but i does not work (though the xml query in the site works but i need json). Anyone has a query that works so i can follow it?
http://inasafe.org/en/developer-docs/osm_building_downloads.html
http://overpass-api.de/api/interpreter?data=[out:json];(node[%23building%22=%22yes%22](-6.185440796831979,106.82374835014343,-6.178966266481431,106.83127999305725);way[%22building%22=%22yes%22](-6.185440796831979,106.82374835014343,-6.178966266481431,106.83127999305725);relation[%22building%22=%22yes%22](-6.185440796831979,106.82374835014343,-6.178966266481431,106.83127999305725););(._;%3E;);out%20body;

Places Autocomplete API to get GPS coordinates from address entered

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).

Categories

Resources