How to get distance between two points from waze - android

i am developing an app in android that must recieve the distance between two points from the user after traveling between them, and that input must be validated, for that i'm thinking of using an API to get the information and make the validations later. I know that it's not the op to ask the user to enter that kind of input, but that's not on me.
For example, if the distance between point A and point B is 13 km according Google Maps Distance Matrix API, i got from the waze api like 18 km. The approach of the app is one about transport so i would prefer to use waze information because it's more accuarate in terms of road traffic.
So, there's any way to take the distance between two points from waze?
more exactly, this one:
Any help would be appreciated
PD: sorry for bad english

I'm not certain whether you're still looking for an answer, but in theory you could call the Waze routing server in the same way as the Waze Live Map calls it.
The following URL is the request sent to the Waze routing server when I calculate the route between Antwerp and Brussels in Belgium.
https://www.waze.com/row-RoutingManager/routingRequest?from=x:4.4024643+y:51.2194475&to=x:4.3517103+y:50.8503396&at=0&returnJSON=true&returnGeometries=false&returnInstructions=false&timeout=60000&nPaths=1&clientVersion=4.0.0&options=AVOID_TRAILS:t,ALLOW_UTURNS:t
Note: make sure to properly encode this URL when using a script. Those colons and commas might cause issues otherwise.
To calculate the length of a route, you'll need to go through all the traversed road segments and make a sum of the length fields (expressed in metres, if I'm not mistaken).
Do note that the results will differ from time to time as Waze includes historical traffic data in its results and will decide to make detours, which may not be wanted for your use case.
I would also strongly suggest to look into the legal status of doing this, though I suspect it won't be a big issue if you're not using this data to make a competing service or 'forget' to mention you're using Waze data to calculate things.

Related

Get total "Areas/Zones" in which I am currently in, based on Current Location

Let us suppose we have three hotels:
Hotel A (popular in 1 km radius), Hotel B (popular in 2 km radius) and Hotel C (popular in 4 km radius). A car enters and is at some position. These Hotels (or any other place) is added by us and is custom.
Problem is I want to find the hotels which have popularity/influence at my current location.
And I want it to do totally with the help of Google Maps. Is it feasible ? on Android (optional)? Please ELI5.
Google Maps API has a function called computeDistanceBetween. It "returns the distance, in meters, between two latitude/longitude coordinates". Circle has the Center and Radius properties. So you need to calculate the distance between the center of a circle and you current location. If it's less than the radius of the circle, than it means your current location is within the circle.
Simplifying is key
Although I understand your objective (get a list of all hotels near you) I believe your explanation threw many people off guard. You don't need to triangulate positions and calculate radius of circles - not with Google Maps APIs and Services.
All you need to know is if you want a solution for front-end, back-end, or mobile
Google Maps Places API Web Service (back-end)
The Places API has a very useful feature called "Places Nearby". To quote the documentation of this feature:
A Nearby Search lets you search for places within a specified area.
You can refine your search request by supplying keywords or specifying
the type of place you are searching for.
Which looks exactly like what you need, right ?
To make a request from a server to the Places API Web Service looking for nearby places, you can do the following:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&name=cruise&key=YOUR_API_KEY
Do remember to change the key=YOUR_API_KEY to a valid key field. The example showed will look for restaurants in a radius of 500 meters around the location of -33.8670522,151.1957362.
There are a lot of parameters to this and you can read more about this in the following documentation
https://developers.google.com/places/web-service/search
Google Maps JavaScript API with Places Library (front-end)
If you however don't have a central server or service to make requests for you, making the clients send the requests directly is also an option.
In this case, there is the JavaScript API. The JavaScript API is a client-side friendly API that re-uses some of the Web Service's features.
In this case, you can use the JavaScript API in conjunction with the Places Library for it. According to the documentation, this API allows you to do "Nearby Search Requests":
A Nearby Search lets you search for places within a specified area by
keyword or type
An example of such a request can be seen in a live example in the following link https://developers.google.com/maps/documentation/javascript/examples/place-search
You can read more about the parameters and usage of this API and this library in the following documentation https://developers.google.com/maps/documentation/javascript/places#place_search_requests
Google Maps Android API (mobile)
From the picture you added, I assume your app will be to "use on the go" (perhaps a mobile app), or something similar.
In this scenario, using a web-server or a website could be cumbersome, as by the time you have a response from it, the car is already in another position !
To aid you in this, there is also the Android API. To use it you need to:
Download and Install Android Studio
Add Google Play Services Package (contains APIs you will use)
You can read more about this process here https://developers.google.com/maps/documentation/android-api/start
As for code and examples, I strongly suggest you check out this GitHub repository of samples
https://github.com/googlemaps/android-samples
Our Hotels may not show up on Google Maps
Unless your DeLorean takes you back to the latter half of the 19th century, or you are stuck in an island with hundreds of Meerkats, poisonous pools and a tiger wondering if he should eat you or not, Google Maps will pretty much always show you some hotels where you can spend the night.
PS: kudos++ if someone gets my references :P
Adding Hotels and Places to Google Maps APIs and Services
If this is still not enough however, there is still a way you can fix it. You can add Hotels addresses and Places to Google Maps by using one for the two following methods:
Send Feedback
Use Maps Maker
Send Feedback
The "Send Feedback" feature allows you to send feedback to Google's data teams for review. Once approved, the data is added to Google's database and will be available to all Google's customers. You can do this by following the steps described here https://support.google.com/maps/answer/3094045?co=GENIE.Platform%3DDesktop&hl=en
Please do note however that the review process for your feedback will take some time, so don't expect anything instantaneous !
Use Maps Maker
Alternatively, you can use Maps Maker. This tool allows you to do edits and add information to our Services in a more streamlined manner.
At first, your edits and suggestions will still be reviewed, and it will usually take less than two weeks to get something approved.
However, with time, as more and more of you suggestiogns are approved, you gain reputation, and when you have a lot of reputation, your suggestions will be pretty much automatically added.
Before using it however, make sure you have a look at the list of supported countries.
Also, if language is important, you may want to consider it as well, by checking the list of supported languages.
I really don't want to add anything to Google Maps
Sometimes the whole business is data itself, so giving it out freely is not an option.
In these cases, you will have you own database, which your services will have to check.
In cases like this a custom solution for your system is needed, but in order to suggest a few more ideas, more information is needed.
Hope it helps !
Your approach should use fast approximate calculations without relying on apis, and then api usage can be used to enhance user experience.
You know the coordinates of the car.
You know the coordinates of each of the hotels.
Assign a popularity weight to each hotel based on your criteria or data you have. e.g a hotel having more user reviews or transactions or bookings will have higher influence, or if you want to personalize, a hotel which suits the loggedin user's budget preferences ( based on past data or settings ) will have higher influence. Lets call this popularity value p1,p2..p3 etc.
Find all hotels that lie within a threshold range, say 5kms within current position of the car. This can be done using a geospatial query in any major storage database ( for example if your hotel points are stored in MySQL, or mongodb ), or if you are using a hotel data api, get the nearby hotels,or all hotels of that city, and prune them based on distance from car's current location.
For linear distance between the car C, and the hotels H1, H2... use Haversine formula , this will give you distance between the Car and any hotel along the Earths curvature. ( Actual road distance might vary, as roads are directions aren't straight and involve turns etc). But this will give you a fast approximation of distances D1 between C & H1, D2 between C & H2 etc...
Now decay the popularity score P1...PN of each hotel based on distance between the Car C and the hotel H. For example if hotel H1 has popularity score P1= 90, and hotel H2 has popularity score P2=90, but C <---> H1 distance is 10kms, and C<--->H2 distance is 2kms,
then H2 will have more influence on the current location compared to H1.
A simple formula can be LocationInfluence = PopularityScore/ log(Distance), you can optimize this based on your use case.
Now for the most influential hotels H1,H2,H3... use DistanceMatrix api to find actual driving distance, you can also use google maps directions api for Android or Javascript to show driving directions to the user, from location till the hotel.

How do I plan a users routing in chronological order?

STILL DESPERATELY LOOKING FOR ANSWERS
I have integrated Google Map's dynamic geocoding in my android project(when the user types an address, it goes there and places a marker). My plan however is to let someone else plan the users schedule for the day.
For example, someones mother wants her son(the user) to do some errands for her. So she opens the app on her phone and types in the places she wants her son to go. The son get the places he needs to go in a chronological order, i.e, the vacuum cleaner store, the hardware depot and then the supermarket. First, the route to the vacuum cleaner store(from his current location) shows up. He goes there, an then presses "done". Then the route from his location to the hardware depot is shown,and so on and so forth.
Not necessary, but appreciated: He should be able to open a navigation drawer with all the errands in a list format, from the first errand to the last.
To summarize, how do I plan someones route in chronological order? I know I am asking for a lot, but if anyone could help me out, that would be great. I would literally sing your praises.
Thanks for reading this wall of text!
It is just an idea, maybe you can try to use waypoints under the Google Maps Directions API.
You can use this waypoints to calculate the routes through additional location.
Here is an example request using waypoints in Direction API
https://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&key=YOUR_API_KEY
You can supply one or more locations separated by the pipe character (|), in the form of an address, latitude/longitude coordinates, or a place ID.
Although this is in JavaScript, this is an example on how to use waypoints.

How do I find nearby app users in android?

I am making an application which needs to be able to find people nearby, who are users of my app.
I looked at many answers of precedent similar questions, and it seems I have no choice but to keep uploading a user's current location to the server, and get nearby users' list when necessary.
Then my question is,
1. To get the nearby list, there should be some algorithm or function which calculates the distance. Then doesn't that mean I have to get all distances between my location and the rest of the app users? So the server returns certain number of users who have the least distance results. If I'm correct, wouldn't there be memory or time issues?
2. This might sound weird, but how about this.
I'll probably send latitude and longitude information or address information to the server. Can't I compare those strings with all users' address list from the first numbers or letters using string searching algorithms or something?
For example, if my last updated address is 'abcde' on the server, the algorithm will look for addresses that start with 'a', if finished searching, then look for addresses that has 'b' after 'a', in other words 'ab'.
This might not be a right solution, but I thought it might work because the address will be saved in same forms.
To find nearby users efficiently, you need a spatial index. See: Hierarchical Triangular Mesh.
You also can use one of the databases that support spatial queries.
I'll probably send latitude and longitude information or address information to the server. Can't I compare those strings with all users' address list from the first numbers or letters using string searching algorithms or something?
That won't work with latitude and longitude because that way you can only search for proximity in one dimension. For example, 30°N 30°E will appear closer to 30°N 90°E than to 31°N 30°E.
It may work with addresses, but only if they are reliably connected with coordinates (i.e. not typed in by users), and only if you don't mind that users 200 meters apart but on different sides of some administrative border will not count as close to each other.
You can use the REST Api from Server Side using PHP which takes the Current LAT LONG of all user for your app From User's Android Phone at certain time Interval Lets Say 5 min & it returns the Nearest Location,Address Distance.You need to call the Api # certain time Interval & in response From Server You will get all the Details What You Want Like Nearest Location of Your App user, Distance and Other Thing.
Why i am suggesting this way because to do Computation from Android Side Will Affect the App Performance Having Battery Issues So its better to Have All computation from Server side instead of Android(User) Side
hope this will helps you.
I am a bit late here but for anyone who comes across this question, assuming you are using Firebase for your database, your best option would be GeoFire using queries around a specific point in coordinates and a radius in km. Here is a link to the github repo https://github.com/firebase/geofire-android which explains everything in detail.

How to get an use an unique identifier for each street in google maps api

My objective here is to identify while the user is asking for directions, if the user has already been in one of the streets (even if not in the exact same coordinates) that were given in the result of the google maps directions search.
It happens like this:
When the user is just browsing the streets, the app, from time to time, asks google where the user is, using the geocode api. And then it registers it (I still need to know which information should I use).
On another time in the future, the user will ask for directions to a specific place he wants to go, the app will ask for alternatives to google maps, then it will decide which path is the one to use having into account if the user will recognize as much as possible as he progresses or the other way around, according to a configuration.
The problem is that I have no clue what to use as the unique identifier for each street.
Note: For now I'm not dividing the big streets into sectors. It should be easier that way.
Ofc, if that's easier than considering the whole street, I'll divide the streets into sectors.
I already tested that i cannot use the coordinates of where the user is. In order not to overlap with near streets and to give a good precision, I'm unable to use the coordinates, even if truncated.
I also checked that using the name of the place that the geocode returns does not seem usable when I'm asking for directions. The directions api does not name the streets with the same identifier...
Help please.
Thanks in advance.
I know you've probably moved on or figured this out by now, but I'll tell you what I would do.
If I wanted to identify streets that might be familiar, I would identify blocks on the street as a set of start and end coordinates. You could check for proximity to the ends of the blocks.
You should be able to find the ends of a block by geocoding the address of a point on the block and split the address line string up by spaces. parse the first number into an int, subtract the modulus of the street number divided by 100, then add one for the beginning and 99 for the end.
String[] splitAddr = address.getAddressLine(0).toString().split("\\s");
houseNum = splitAddr[0];
street = address.getAddressLine(0).toString().substring(houseNum.length());
blockNum = Integer.parseInt(houseNum);
blockNum = (blockNum - (blockNum % 100));
blockstart = blockNum + 1;
blockEnd = blockNum + 99

Creating a random lat/long coordinate that is positioned in a physically reachable location

I have an application that generates some number of tokens around the current location of the user using a certain distance radius. The user will then have to run to some of those locations. The problem is that some tokens can be created in a lake,forest,ocean, or some other physically unreachable location. As a quick fix I just generate extra tokens and increase the proximity distance that determines if a user reached a certain location. I now want to improve this so that each token is located at a reachable location.
The only solution I have been able to come up with is using the Google Directions API to determine a path from the user to the token and use the last coordinate in the polyline as the new reachable location of the token. My problem with this is that I potentially have to post up to 30 requests to the Directions service simultaneously and I am worried that I might hit the query rate limit. I have not found anything definite about query rate limit.
So my question is whether anyone knows of a better solution or can give any input on the Directions query rate limit? Waiting 1 second between each request and forcing the user to wait up to 30 seconds is not a reasonable solution. Thanks.
UPDATE
Using the solution that I described in the question does produces an OVER_QUERY_LIMIT, even if I wait 1 second between each request. Other then that the logic was sound and tokens that got a request thru were appearing in walk reachable locations.
You can calculate the distance between two lat/lon with Location.distanceBetween(). This is a static convenience API call. There's no limit on number of calulations.
Usage limits
Use of the Google Directions API is subject to a query limit of 2,500 directions requests per day. Individual directions requests may contain up to 8 intermediate waypoints in the request.
Google Maps Premier customers may query up to 100,000 directions requests per day, with up to 23 waypoints allowed in each request.
You might want to take a look into the Maps premier customer so you don't hit the limit too fast. If your app becomes popular I bet you can get an higher limit.
One way would be to see if you can see the elevation to see if it's a cliff or not. However just seeing if it's a lake or not seems to be quite hard. Might be some kind of gps lookup service out there except Google Maps.
If you just want to know the distance between locations just use Location.distanceTo() or static distanceBetween()
Getting the info if it's a road or not is another question.
After some 8 hours I finally got something working. So using the fact that each request can have up to 8 waypoints I can technically ask directions to 9 locations in one request. Here's what I am doing now:
Generate 9 random locations at a time. Pass the locations to my DirectionsComputer which returns the polyline path that goes through all 9 coordinates as a list of coordinates. Then I pick 9 location from the path and set them as the locations of my tokens. Now all my tokens are semi-randomly generated, always appear on a road, and I only need to do 4 consecutive requests to generate 36 tokens.
There are some cases where a few tokens are bunched together. For example, the coordinates which are originally located in the ocean get moved to the same beach. But for the most part, all my tests showed the tokens spread apart and I could tweak the rest.

Categories

Resources