My current code use FusedLocationProviderApi to get user's current city/locality. This code is in my Service :
try {
Geocoder gcd = new Geocoder(LocationUpdateService.this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0)
{
txtStation.setText("Locality : " + addresses.get(0).getLocality() + " Sublocal : " + addresses.get(0).getSubLocality());
}
}catch (Exception e) {
Toast.makeText(LocationUpdateService.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
However i need to show user's more specific place (bus station, gas station, etc) and i think i must use Places API.
How to integrate the Places API to my current code (which use FusedLocationProviderApi)?
What i think now is to use FusedLocationProviderApi to get the basic latitude & longitude, and then use Places API with those coordinates.
Please kindly help me/show me some clue
Thanks a lot for your help
How about the PlaceDetectionApi?
Related
I am new to Android development, following is my code about use Geocoder to get city name of current location, it returns null:
private void updateCurrentLocation(Location location) {
double lat = 0.0, lng = 0.0;
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
Log.i("tag", "Latitute is" + lat + ", Longtitute is" + lng);
} else {
City_Name = "Unavailable";
}
List<Address> list = null;
Geocoder geocoder = new Geocoder(this.getActivity());
try {
list = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
e.printStackTrace();
}
//may provide multiple locations.
if (list != null && list.size() > 0) {
Address address = list.get(0);
City_Name = address.getLocality();
}
Log.i("Try", "CityName:" + City_Name);
//send empty message
handler.sendEmptyMessage(1);
}
I opened GPS services, add ACCESS_FINE_LOCATION and INTERNET permission in Manifest already. Also, I searched similar questions in Stackoverflow about Geocoder returns null, but haven't found useful solutions. One of them is analyze JSON from Geocoder website, but it doesn't work either.
Can anyone help with this? Thank you!
BTW, is there any better solution to receive a city name? Thank you!
If the "getFromLocation" method gives you an empty set then it's because the server that is being looked up doesn't have the address information for the coordinates you're passing it. This is also noted in the docs. So I think that you should let it go and use another service like the Google Maps geocoding service or another one like Nominatim from the OpenStreetMap project.
Here I am using this code in order to get the latitude and longitude of the location but all that I am getting is latitude 0.0 and longitude 0.0 the code to find the latitude and logitude that I am using is given as below , Also I am testing this code in Genymotion emulator and all that is printed in the log is 0.0 0.0 because the if(address.size()>0) is not getting satisfyied can anyone tell where am I going wrong or is it because of emulator.
Geocoder geo = new Geocoder(getActivity());
try {
List<Address> address = geo.getFromLocationName(str_ELocation,1);
if (address.size() > 0) {
Log.d("List Size : ", "" + address.size());
latitude = address.get(0).getLatitude();
longitude = address.get(0).getLongitude();
} else {
Log.d("In Else", "In Else");
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
Log.d("LONGITUDE AND LATITUDE", "" + longitude + "" + latitude);
Also is there any other simpler solution to just find latitude and longitude. I am using this in Fragment and so I am using getActivity() here instead of myClassName.this.
I'm guessing you don't have a backend service you are using to obtain this information.
Note The Docs say
The Geocoder class requires a backend service that is not included in
the core android framework. The Geocoder query methods will return an
empty list if there no backend service in the platform. Use the
isPresent() method to determine whether a Geocoder implementation
exists.
This should also be done on a background Thread if not already being done.
Alternative
You may be better off using LocationManager
Be sure to read those docs on it before beginning to have an understanding of how it works and should be implemented (That portion is short).
Here is an example that can get you started
Anything like this should be done on a background Thread and not on the UI Thread. And there are listeners which can return the values you need.
full disclosure: I haven't used GPS but I read through the docs ;)
I need to fetch longitude and latitude by street name. I saw that Geocoder can do that job. The documentation tells us that "the Geocoder class requires a backend service that is not included in the core android framework". That means that I can't be sure that Geocoder is implemented in Android devices.
My question is, how widespread is the Geocoder implementation. If there are only very few devices without Geocoder, I can live with that, but if there are a lot of devices, I have to rethink this functionality of my app.
We should use isPresent() method of GeoCoder class to decide so.
http://developer.android.com/reference/android/location/Geocoder.html#isPresent()
A GeoCoder client is included along with Google Maps, so any device with Google Maps should have a working GeoCoder implementation. By and large, Android devices have Google Maps installed. Other than that, there could be no Google Maps, and some other GeoCoder backend available. In such a case, you can use the following code to determine if the device has a working GeoCoder implementation:
final Geocoder geocoder = new Geocoder(this);
final String locName = "1600 Amphitheatre Parkway, Mountain View, CA";
try {
final List<Address> list = geocoder.getFromLocationName(locName, 1);
if ( ! (list == null || list.isEmpty()) ) {
final Address address = list.get(0);
System.out.println("Geocoder backend present, (lat,lon) = (" + address.getLatitude() + ", " + address.getLongitude() + ")");
}
else {
System.out.println("Geocoder backend not present");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i want to get the latitude and longitude using the zipcode for android application
This is a much simpler solution, assuming the geocoder services are present:
final Geocoder geocoder = new Geocoder(this);
final String zip = "90210";
try {
List<Address> addresses = geocoder.getFromLocationName(zipCode, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
// Use the address as needed
String message = String.format("Latitude: %f, Longitude: %f",
address.getLatitude(), address.getLongitude());
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
} else {
// Display appropriate message when Geocoder services are not available
Toast.makeToast(this, "Unable to geocode zipcode", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// handle exception
}
There is no rival of Google in GeoCoding :)
You have to just make an http Get request for the following link.
Here's a link
Just change the postal code Element According to your requirements. And Enjoy.
For further reference follow the link.Maps Geocoding Reference
You could use YQL like so: select centroid from geo.places where text="ZIPCODE_HERE"
You will have to use a Geocoding Service, like the ones provided by Google or the freely available Nominatim. Note that since you will be just providing a ZIP code, you might not get the results you want due to the inaccuracy of the address itself.
According to the Geocoder documentation, the class provides methods into a backend service which may or may not exist on the device. If the geocoder doesn't exist on the device, theoretically a geocode request will return no results. However, how can one differentiate between no results due to there being no backend geocoder service vs. there being no results because the location makes no sense?
My app already requires the Google Maps API, so perhaps this is a moot point; as a secondary question, does the Google Maps API always include a Geocoder?
Strictly speaking there could be another backend installed, just try if a geocoder is present
final Geocoder geocoder = new Geocoder(this);
final String locName = "1600 Amphitheatre Parkway, Mountain View, CA";
try {
final List<Address> list = geocoder.getFromLocationName(locName, 1);
if ( ! (list == null || list.isEmpty()) ) {
final Address address = list.get(0);
System.out.println("Geocoder backend present, (lat,lon) = (" + address.getLatitude() + ", " + address.getLongitude() + ")");
}
else {
System.out.println("Geocoder backend not present");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
According to the Google Maps docs, "The Google Maps API provides a client geocoder for geocoding addresses". So I would say that since you are requiring the google maps API, you are requiring a geocoder.