Wrong reading from Google Place Android API - android

i have developed an application that interacts with google places api to search for cafe in the neighborhood of a user. for the purpose of my application i want to have the radius set to a very small value.. may be 20 to 50 meters. currently my app is working but its not working the way it should.
In my app, i have set the radius to 350 meters. so practically it should return me the list of cafe that fall in the radius of 350m. however my app still shows starbucks in this result set even if starbucks is 650m (road distance) and 500m (straight distance). I would like to know what should be done to get the correct results.
Currently i am using GPS for this. will a combination of gps and network service provide me a better reading?
public PlacesList search(double latitude, double longitude, double radius, String types)
throws Exception {
this._latitude = latitude;
this._longitude = longitude;
this._radius = radius;

this question is not related to Android, but to the Google Places API.
are you using place search or text search? both of them have comments regarding location biasing
text search:
"You may bias results to a specified circle by passing a location and a radius parameter. This will instruct the Place service to prefer showing results within that circle; results outside of the defined area may still be displayed."
place search:
"distance. This option sorts results in ascending order by their distance from the specified location. Ranking results by distance will set a fixed search radius of 50km. One or more of keyword, name, or types is required."

Related

How to overcome google static maps API 6 decimal places precision limit?

Google Static Maps API Documentation states:
Latitudes and longitudes are defined using numerals within a comma-separated text string that have a precision to 6 decimal places. For example, "40.714728,-73.998672" is a valid geocode value. Precision beyond the 6 decimal places is ignored.
However, I have noted that in many cases, that precision is not enough.
(Edit: Actually, 6 decimal places allows a precision of approximately 2 cms, as 323go comments. See the edit at the bottom for further info)
E.g: Trying to put a marker on the Eiffel Tower (Lat: 48.8583701,Lon: 2.2922926) gets truncated (to Lat: 48.858370, Lon:2.292292) obtaining the following result, which has a non negligible offset:
I use static maps is because in my application I show multiple maps simultaneously inside the items of a RecyclerView.
I currently achieve that by asynchronously injecting the images returned by the Google static maps API via Picasso.
This current approach works well and performs smoothly, the only problem being the lack of precision of the map.
As a workaround, I am considering using the standard MapView in Lite Mode, but I am concerned that it could lead to performance issues, as stated in this question
Is there a way to overcome that limit, even if it requires paying?
Edit
I was using wrong coordinates. I'll explain how I got them, just in case anyone else makes the same mistake.
I was using the coordinates that appear in the URL after loading https://google.com/maps/place/Tour+Eiffel, which in my case is this URL.
When the left side panel of the web version of Google Maps is open (which is the default behaviour), the pin appears to be in the center of the map.
Nevertheless, the coordinates of the URL represent the center of the map including the part under the left panel. This is easy to notice once the left panel is collapsed.
This is what caused the horizontal offset.
Im trying to figure this out but on my side everything is working fine and precise, i noticed you are using different coordinates than mine
For the Eiffel Tower i used:
48.858285, 2.294388
That should give you a better result, also remember you can place a marker with the name of the place or with the full address with Geocoder which is:
Champ de Mars, 5 Avenue Anatole France, 75007 Paris, France
Something like this should help
Geocoder geocoder = new Geocoder(<your context>);
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0) {
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}

is it possible to get distance between current location and a street, avenue or area?

all solutions about calculating distance are between two points.
my question is:
Is there any way to calculate the distance to a certain street?
suppose I want to calculate a distance from my location to a specific area or avenue, which have many streets to get there, in this case how to know the nearest street to me to get there?
here I have only one point (my location) while the destination point is variant and depends on my current location. what I know about destination is in a certain street or area, so how to calculate it?
another case for this issue, suppose I am away 10 meters from a certain street which is 1 km long.
I should be able to calculate that my distance is 10 meters from the beginning of that street, after this distance when enter the street, and for each meter I waked for 1 km it should be 0 meter distance from my location to this street, and when I exit it from the other side the distance should be calculating again from the end point of that street not from the beginning point, so when I get away from it about 5 meters it should calculate it as 5 meters not as 1005 meters.
Here is the requirement by example on google maps:
first let's see Wall Street in NY city on map.
So the target destination is Wall Street.
Now if your current location in the cross of John Street with Water Street, my task is to lead you to the nearest point in Wall Street and the path will be as this one (walk mode).
however if you are in the cross of Broad Street with Beaver Street then the destination to nearest point in Wall Street will be like this one (walk mode):
this is my issue
is it possible? Do new APIs offer a solution for this?
any solutions or ideas?
hey yes you get latitude and longitude of searched location here i'll example how you can get.
You can use google autocomplete for searching any place.Then get placeid from autocomplete api then pass it to details api.
https://maps.googleapis.com/maps/api/place/autocomplete/json
https://maps.googleapis.com/maps/api/place/details/json
See pass the parameters
//Google Autocomplete
#GET(ApiConstants.GET_AUTOCOMPLETE)
Observable<GoogleAutocompleteSearchResponse> getAutoCompleteSearch(#Query("input") String input, #Query("key") String key, #Query("components") String components);
// Google Autocomplete places
#GET(ApiConstants.GET_AUTOCOMPLETE_PLACE)
Observable<GoogleAutoCompletePlacesResponse> getAutoCompletePlaces(#Query("placeid") String input, #Query("key") String key);
You can get latitude longitude in second one api.
Hope this will help you.
First get the longitude, latitude of destination location
public Location getCoordinatesFromAddress(Context context,String strAddress) {
Geocoder coder = new Geocoder(context);
List<Address> address;
Location destinationLoc = null;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
destinationLoc = new Location("Destination");
destinationLoc.setLatitude(location.getLatitude());
destinationLoc.setLongitude(location.getLongitude());
} catch (Exception ex) {
ex.printStackTrace();
}
return destinationLoc;
}
As you mentioned you have latitude and longitude of your present location. Use that to create your current location.
Location locationA = new Location("Present location ");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Next after getting latitudes and longitudes of your destination and source(present) locations you can easily calculate the distance between them
e.g
float distance = locationA.distanceTo(locationB);
It will give you the distance b/w two geo locations in meters.
Take a look at this response. Also take a look over this API from google.
I guess what you want looks like this:
- get targeted street coordinates [lat, long]
- get nearby places from google,
- search the nearest place.
In this case, if you can get address of both destination and origin, then by following method you can get distance between address.
For which you need to pass either address (origin/destination) or latitude / longitude (origin/destination)
1) Here the origin parameter can be like following , if address is provided
origins=Bobcaygeon+ON|24+Sussex+Drive+Ottawa+ON
2) Where as , if latitude / longitude is provided , then following will be the format.
origins=41.43206,-81.38992
3) If you are able to get place ID , then this will be the format
origins=place_id:ChIJ3S-JXmauEmsRUcIaWtf4MzE
The following example uses latitude/longitude coordinates to specify the destination coordinates
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626&key=YOUR_API_KEY
Same request for encoded polyline will be
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=enc:_kjwFjtsbMt%60EgnKcqLcaOzkGari%40naPxhVg%7CJjjb%40cqLcaOzkGari%40naPxhV:&key=YOUR_API_KEY
As explained by other's the general method to find shortest distance using API is
float distance = firstLocation.distanceTo(secondLocation);
For more information go through this link
Just use Haversine formula to calculate distance between two points if you know the latitude and longitude for each of them.
Node Package: Haversine Node
Gradle: Gradle Haversine

Figure out Latitude and Longitude

I am trying to develop an android application that can do task according to the lat/long. This is something like if I am in particular suburb (Lets say I am in Belconnen, ACT Australia, I would like to get the details of that place automatically) - if I move out the border of belconnen then I have to show some other details.
If you check this google maps link: http://goo.gl/4mItcF you would see the red border is only belconnen suburb.
My question is how do I give the borders in my App (meaning how do I tell my App that I am now in belconnen, ACT? Is it by getting lat/long along the borders store them in DB and check if I am inside required lat/long, if that is the case DB would have huge numbers only for Belconnen, ACT right?
Or is there an easier way to get the borders?
Let me know!
Thanks!
There is a way , it is called Reverse Geocoding. You can use it by two ways :
1. Using Geocoder class of Android -
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
Here you use lat = -35.2374551 and lng = 149.0672515 for Belconnen, ACT Australia. Check out the function getFromLocation for more information.
2. Using Google REST Web Services :
https://maps.googleapis.com/maps/api/geocode/json?latlng=-35.2374551,149.0672515 (JSON)
https://maps.googleapis.com/maps/api/geocode/xml?latlng=-35.2374551,149.0672515 (XML)
pass comma separated latitude and longitude and u will get the most precise location for that in response. Find more information here Reverse Geocoding google API
As soon as you go out of Belconnen, ACT Australia, the address in response will no longer contain this and you can put some logic to get your desired behavior.
As far as i understand, you would like to create a Location Aware Application.
As per the Android documentation, Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app.
Step 1 - Knowing the current Location of the User
Google recommends to use FusionProvider API for this purpose. It is one of the location APIs in Google Play services where you can specify requirements at a high level, like high accuracy or low power. It also optimizes the device's use of battery power.
https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html
Step 2 - Based on the latitude and Longitude information retrieved in the location object, you can use the below Google web service to get the postal code of the location.
http://maps.googleapis.com/maps/api/geocode/json?latlng=lattitude,longitude&sensor=true
Step 3 - How Frequent can we retrieve the location information
Turning on the listeners continuously will impact the device battery highly. So, to avoid this, we need to minimize the frequency of retrieving the location information.
I would recommend to measure the distance traveled by the user and based on that set the next location update trigger. Since you are concerned about boundary only, we can set a high value based on the device acceleration.
Refer the below links for more details on how to implement this effectively.
https://stackoverflow.com/a/11644809/623517
Hope this helps.
You can get full address from lat long and then search your city or location. This answer explain how to do it : How to get complete address from latitude and longitude?
If you want further flexibility you can use geofence : http://developer.android.com/training/location/geofencing.html
You can approximate the region with LatLngBounds (com.google.android.gms.maps.model.LatLngBounds).
Use it like this:
LatLngBounds.Builder builder = new Builder();
for (int i=0; i<points.length; i++) {
builder.include(points[i]);
}
LatLngBounds bounds = builder.build();
And then check if your location falls inside it:
if (bounds.contains(myLatLng)) {
//your implementation
}
I know this approach cannot give perfect results but it seems to be more efficient then using the geofences.
LocationManager lm;
lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria c=new Criteria();
String provider=lm.getBestProvider(c, false);
Location l=lm.getLastKnownLocation(provider);
double latitude = l.getLatitude(); // latitude
double longitude = l.getLongitude(); // longitude

Google place api, wants to get nearest location

Hello I want to search nearest location using google place api....
from https://developers.google.com/places/documentation/supported_types i have used types in my code for searching nearest places....
and the code is below
try {
double radius = 5000; // 10000 meters
// get nearest places
nearPlaces = googlePlaces
.search(gps.getLatitude(), gps.getLongitude(), radius,
"sublocality|sublocality_level_4|sublocality_level_5|sublocality_level_3|sublocality_level_2|sublocality_level_1|neighborhood|locality|sublocality");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
but could not find nearest places
like if i am in "San Diego, CA, USA" then i want to get result like
Gaslamp/Downtown,
Pacific Beach,
Ocean Beach,
Uptown,
Hillcrest,
Mission Valley,
Fashion Valley,
North Park,
but instead of this i am getting only two value like
Mission Valley East,
Serra Mesa
so can any one help me what type i have to pass so i can get result like above instead of below result
My previous answer was deleted by a moderator for unknown reasons, I am re-posting with some more context as it is the currently the correct answer:
It appears you are trying return nearby geographical locations (anything from the second list in the supported place types). Unfortunately the Places API is not designed for this, it is designed to return a list of nearby establishments (anything from the first list in the supported place types) and up to two locality or political type results to to help identify the area you are performing a Place Search request for.
This is stated in the documentation here:
https://developers.google.com/places/documentation/search#PlaceSearchResponses
"results" contains an array of Places, with information about each. See Place Search Results for information about these results. The Places API returns up to 20 establishment results per query. Additionally, political results may be returned which serve to identify the area of the request.
If you type filter your Place Search request by a geographic location type like locality or political, you will filter out the establishment results and only be left with the area identity results.
A Places API - Feature Request for returning nearby localities has been filed here:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=4434
If you believe this would be a useful feature please make sure your star the request to let us know you are interested in seeing it added.
double radius = 5000 is 5 Kilometers just over 3 miles.
If you increase the radius you should return more places
You should consider the distance to maximum 50Km and there are different types as follows:
establishment,lodging,spa,bar,church,place_of_worship etc.

How can I find the distance from a particular latitude and longitude to surrounding area in km?

I am new to Android. In my application, customer is at a location. I want to find agents near-by to that customer using his/her latitude and longitude. How can I do this? customer is on one location & We want to search agents surronding that perticular customer.
I have latitude of customer and agents and based on I want to search agents(From customer's latitude longitude in surrounding area of 5 km. which agents are thare that i want to search).
pseudocode
area = 100;
for a over allAgents
if(Math.abs(a.x - customer.x) < area || Math.abs(a.y - customer.y) < area)
nearCustomerArray.add(a);
If you already know agent's location, grab you current location using built-in device gps receiver.
Then, calculate distance between your coordinates and the one of the agent using distanceTo method of Location class
Finally, found the small distance among all the distances you calculated.

Categories

Resources