Latitude and longitude addresses for Google Maps always show Chinese locations - android

The Problem
I'm trying to retrieve latitude and longitude from an address using Geocoder.getFromLocationName().
It works perfectly for some Android devices, but it keeps showing Chinese latitude and longitude addresses for my Xiamoi mobile phone. However, it should be showing my location - Saket, Delhi, India.
Here's the code:
public LatLng getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(getActivity(), Locale.getDefault());
List<Address> address;
LatLng p1 = null;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new LatLng(location.getLatitude(), location.getLongitude());
return p1;
}
}

Change the Locale
The Locale passed into the Geocoder constructor represents the desired Locale for the query results. This applies the the following code:
Geocoder coder = new Geocoder(getActivity(), Locale.getDefault());
Be wary of the default locale
From the documentation for Locale.getDefault();
Note that there are many convenience methods that automatically use the default locale, but using them may lead to subtle bugs.
A common mistake is to implicitly use the default locale when producing output meant to be machine-readable. This tends to work on the developer's test devices (especially because so many developers use en_US), but fails when run on a device whose user is in a more complex locale.
The default Locale is most likely China. This makes sense seeing as Xiaomi is a Chinese manufacturer. This is probably why you keep getting Chinese addresses.
Test
Try the following and check your logs to see what the locale is being set to:
Locale defaultLocale = Locale.getDefault();
Log.i("MyActivity", defaultLocale.toString());

Related

How can I get values from address of Place Picker?

I want to get values from address of Place Picker in android and separate it into different EditText like I want to set pincode on PineCodeEditText, set country on CountryEditText, set State on StateEditText and so on. I have got address as
10, Mothorowala Rd, Jagdamba Colony, Dharmpur, Ajabpur Kalan,
Dehradun, Uttarakhand 248001, India
Where I used
Place place = PlacePicker.getPlace(data,this);
String address = String.format(String.valueOf(place.getAddress()));
Before this I used Split function but it gives indexOutOfBoundException.
Any suggestion please welcome, Thank You.
You can get the latitude and longitude coordinates from the Place object, then get the address object using the GeoCoder, which you can use to get the required data separately.
LatLng coordinates = place.getLatLng(); // Get the coordinates from your place
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(
coordinates.latitude,
coordinates.longitude,
1); // Only retrieve 1 address
Address address = addresses.get(0);
String countryCode = address.getCountryCode();
String countryName = address.getCountryName();

Geocoder APIs returning range of addresses

I am attempting to use latitude and longitude coordinates to resolve a specific address. Currently, the Geocoder APIs will occasionally return a range of addresses, even if the selected location is an individual house rather than something like an apartment building.
The below method accepts a latitude, longitude, and Activity and returns the Address object returned from the Geocoder API:
public static Address resolveAddress(double latitude, double longitude, Activity activity) {
Geocoder geocoder = new Geocoder(activity);
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0 ) {
Log.d(TAG, "resolveAddress: " + addresses.get(0).toString());
return addresses.get(0);
}
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
return null;
}
The getFromLocation() method provided by the Geocoder API accepts a maxResults parameter as it's last argument, which you can see in the above code is set to 1. Even so, while only a single result is returned, a range of street numbers is returned in the single result.
I would assume that GPS and the Android Location service is accurate enough to resolve to a single house. How can I use latitude and longitude coordinates to best determine a single address?

I want the street name and address with logitute and latitude as shown in image in android

I want the street name and longitude latitude of entered pin code in edit text as shown in the image of google map. I want to develop this type of feature in android.
Thank you in advance.
Try this,
Geocoder geo = new Geocoder(getApplicationContext(),
Locale.getDefault());
List<Address> add;
try {
add = geo.getFromLocation(taplat, taplon, 1);
addstr = add.get(0).getPostalCode();
}

Getting Location from coordinates

I have given coordinates for latitude and longitude, and I want make a location object with those.There isn't a constructor that takes to doubles to make a location, so I tried like this:
Location l = null;
l.setLatitude(lat);
l.setLongitude(lon);
but it crashes. Any different idea?
Just create a new Location with new Location("reverseGeocoded");
like this
GeoPoint newCurrent = new GeoPoint(59529200, 18071400);
Location current = new Location("reverseGeocoded");
current.setLatitude(newCurrent.getLatitudeE6() / 1e6);
current.setLongitude(newCurrent.getLongitudeE6() / 1e6);
current.setAccuracy(3333);
current.setBearing(333);
This crashes because you cannot call methods on an non existent object.
I presume you are talking about android.location.Location?
This is usually returned by the various positioning services of Android.
What do you want to do with it?
Do you want to reverse geocode it? As in find an address for that geo coordinate?
Or do you want to use it as a "fake" position and feed it to other applications?
There are BTW two constructors. One takes the name of a positioning service and the other is a copy constructor and takes an existing Location.
So you could create a Location like this:
Location l = new Location("network");
But I do not think that this will result in something you want to have.
Here is a link to the documentation:
https://developer.android.com/reference/android/location/Location.html#Location%28java.lang.String%29
Location l = new Location("any string");
l.setLatitude(lat);
l.setLongitude(lon);
Try this:
public double lat;
public double lon;
public void onLocationChanged(Location loc)
{
lat=loc.getLatitude();
lon=loc.getLongitude();
Toast.makeText(getBaseContext(),"Latitude:" + lat +Longitude"+lon,Toast.LENGTH_LONG).show();
}

Android getFromLocationName return results outside the bounding box

I am doing a location search from my android app. User enter an address and I do a lookup with the following code,
private void doSearch(String query){
FNMApplication.logInfo("Searching:"+query);
//create a geocoder
Geocoder gc = new Geocoder(this,Locale.getDefault());
try{
//lookup locations which match the query input by the user
List<Address> addresses = gc.getFromLocationName(query, 5, -44.00, 111.00, -12.0, 155.0);
//if there are any results save them in an ivar for re-use
locationSearchResults=addresses;
promptSearch();
}
catch (Exception e){
;
}
}
The bounding box above is for australia but if I search for "Los Angelos" it returns results in the US. Is there something I have missed? As I see it, it should only return addresses within the bounding box as per the reference document
From the Question I tried this and the results I got are shown below.
18.55, 72.54 = Mumbai
22.18, 70.56 = Rajkot
1)
When I pass lower latitude vaue as left lower and search for the string I got this
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocationName("akshardham", 5,
18.55, 72.54, 22.18, 70.56);
Log.i("Address", "=========0----------------------"
+ addresses.get(0).getAddressLine(0)
+ addresses.get(0).getAddressLine(1));
Log.i("Address", "=========0----------------------"
+ addresses.get(1).getAddressLine(0)
+ addresses.get(1).getAddressLine(1));
Log.i("Address", "=========0----------------------"
+ addresses.get(2).getAddressLine(0));
Log.i("Address", "=========0----------------------"
+ addresses.get(3).getAddressLine(0));
Log.i("Address", "=========0----------------------"
+ addresses.get(4).getAddressLine(0));
} catch (Exception e) {
;
}
Logcat of this
11-17 12:42:32.419: INFO/Address(802): =========0----------------------AkshardhamNH 8C, Sector 20
11-17 12:42:32.429: INFO/Address(802): =========0----------------------AkshardhamRajkot, Gujarat 360005
11-17 12:42:32.429: INFO/Address(802): =========0----------------------Akshardham
2)
When I pass higher latitude vaue as left lower and search for the string I got this
List<Address> addresses = gc.getFromLocationName("akshardham",5, 22.18, 70.56,18.55, 72.54 );
Logcat of this
11-17 12:43:53.170: INFO/Address(837): =========0----------------------HardhamPulborough, West Sussex RH20 1
3)
The doc here itself says "The addresses matching inside the bounding box is given higher rank." , I think they mean to say, Its not necessary that If search string is address between this box only will be return,if they found the address they will return even if its out of bounding box.
I Suppose;Bounding Box is just for setting the priority of result while getting means result from the box wil be first and and then other like in get(0),get(1) of the list. but the result will be given even if they are not in bounding box.
4)
In the Geocoder.java here they are just calling the method to get addresses with passing double values as argument simple..no any checking for the result is used there else to check the lowest and highest value
=> So final answer to you problem you just call the function as you are calling and you can check the second address line that does they matches to yours means the state or country is same. to find the country,state name of your bounding box value use getFromLocation(latitude, longitude, maxResults), I still Didn't Find A Perfect Solution For this,So me too Searching
=> More on this can be explained by an expert (That's I am not) or Developer from Google itself.

Categories

Resources