keepwalking
keepwalking

Reputation: 2654

Google Maps Directions API - Distance travelled in city and outside city

So the user gives me two starting points A and B. I am able to calculate the total driving distance between those points but i am interested if its possible to get the distance traveled inside a city and outside city.

What i want to find out is the fuel consumption and i have two consumption values. One value is while you drive inside a city and one value while you travel outside. So less traffic, national roads, highways etc.

For example fuel consumption values:

city - 9l/100km
outside - 7l/100km

Distance between A and B

10km inside city
50km outside city

Fuel consumption:

9/100 * 10 + 7/100 * 50

Thanks

Upvotes: 1

Views: 219

Answers (1)

Subrata Mondal
Subrata Mondal

Reputation: 852

You cannot do this automatically by implementing any APIs provided by Google. However, there are some manual ways. You have to make a database of the city boundary:

This link returns a detail of a city. Like, if you search 'New York' the boundary of New York will be shown, like this:

Image

Now if you click the detail button, you will get an OSM id which is '175905' for New York.

Now you have to go to this link and paste this OSM in the Id of relation text box and submit. You will get polyline informations in many formats. Like New York - you have to save the polyline info for each city you want to include in your app.

Now the task becomes easy:

Add this into your dependencies:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.5+'
}

and check if your current location falls under the any of the Polygon from your Database of cities:

PolyUtil.containsLocation(yourLocation, cityPolyPointsList, false);

Upvotes: 1

Related Questions