The Android API has Location.distanceBetween(), which accepts two lat/lon values and returns a distance in meters. Is there a way that I could get this distance with only having a zip (postal) code for one of my points?
You may want to use Android's Geocoder API. Something like this should work:
String locationName = zipCode + ", " + countryName;
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> address = geoCoder.getFromLocationName(locationName, 1);
double latitude = address.get(0).getLatitude();
double longitude = address.get(0).getLongitude();
Location.distanceBetween(...);
} catch (IOException e) {
e.printStackTrace();
}
You need to include the country's name because of this: Get latitude and longitude based on zip using Geocoder class in Android
Related
whenever I click on a particular location in Google Map, I want that location's name to be displayed in a text view. Is there any way I can do it?
Get lat_lng value for particular location, then lat lng value pass to geocoder method.
public void getgetLocationAddress(Context context,double lat,double lng){
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(context, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(lat, lng, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
// System.out.println("SDK_DATA"+address+"..."+city +country);
//Here address set to your textview
} catch (IOException e) {
e.printStackTrace();
} }
There was a question years ago, how to get lat long coordinates using only the adress ( here is the link to the question: How can I find the latitude and longitude from address? )
I understand the accepted answer there, but my problem is, for example, in Germany you have no unique adresses, so if I use only adresses to get lat and long coordinates, I may get wrong lat long coordinates. Like, there is one adress called "Hauptstrasse" which is in Berlin and in Frankfurt. So I will get wrong coordinates.
Is there any way to use Adress AND Zip code to get the right lat long coordinates ?
This code for example only uses adresses:
public GeoPoint getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
(double) (location.getLongitude() * 1E6));
return p1;
}
}
I had the same problem.
This is how I solved this problem.
I have a txt file that contains adresses, zip codes and city names.
To keep your example for Germany: Hauptstrasse 123, 10978, Germany (this will be somewhere in Berlin)
Then, I put this txt file into assets folder and used a BufferedReader and created an Array.
public void readFile(){
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("YOURFILE")));
String line;
while ((line = reader.readLine()) != null) {
String splittedLine [] = line.split(",");
int numberofElements =2;
String[] onlyAdressandZip = Arrays.copyOf(splittedLine, numberofElements);
} catch (IOException e) {
e.printStackTrace();
}
As you can see, I did not use the name of the country (Germany) that's why I used Arrays.copyOf
Hauptstrasse 123, 10978 , Germany has a length of 3 but we only need Hauptstrasse 123 and 10978 that explains int numberofElements =2
That's it and then you can give onlyAdressandZip[0] and onlyAdressandZip[1] as input for the GeoCoder to get the right lat and long coordinates.
In my android app, I have to list all local cities names using my current location with Google Map API
Input
Current Co-ordinates
Current place Name.
Output
Local cities of current place.
You must collect the latitude and longitude first.
then you can user geocoder to get it. Like :-
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());
Try this way
public List<Address> getAddressListFromLatLong(double lat, double lng) {
Geocoder geocoder = new Geocoder(this);
List<Address> list = null;
try {
list = geocoder.getFromLocation(lat, lng, 20);
// 20 is no of address you want to fetch near by the given lat-long
for (Address address : list) {
System.out.println(address);
}
} catch (Throwable e) {
e.printStackTrace();
}
return list;
}
I am new to android mobile development. I have used the Location Manager class and successfully found out the Longitude and the Latitude of the user. I want to use these values to find the city name. I don't want maps, I just want to get the city name. How do I do this?
First get Latitude and Longitude using Location and LocationManager class(That you have completed). Now try the code below for Get the city,address info
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
City info is now in sb. Now convert the sb to String (using sb.toString() ).
https://github.com/commonsguy/cw-lunchlist
https://github.com/commonsguy/cw-android
http://developer.android.com/reference/android/location/LocationManager.html
Have a look at these sites this will help you!!!!!1
You can use the Geocoder
Geocoder myLocation = new Geocoder(context, Locale.getDefault());
List<Address> myList = null;
try {
myList = myLocation.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {}
Where longitude and latitude are the valued retrieved by networks or GPS
Is there any way to get zip-code of the current user location from location or lat or long.
If so how?
Use Android Geocoder API!
getFromLocation(double, double, int) is all you need.
Yes you can...All you need to do is extract the
http://maps.google.com/maps/geo?ll=latitude,longitude
http://maps.google.com/maps/geo?ll=10.345561,123.896932
Or try experimenting this solutions.I can give you an idea.
You can have a city right? WIth this json data, If you have a database(at maxmind.com) of the zipcode with this format
CountryCode | City | ZipCode
PH |Cebu |6000
Now query your database that is relative to the countrycode that googles return.
UPDATE
The Geocoding API v2 has been turned down on September 9th, 2013.
Here's the new URL of API service
http://maps.googleapis.com/maps/api/geocode/json?latlng=your_latitude,your_longitude
http://maps.googleapis.com/maps/api/geocode/json?latlng=10.32,123.90&sensor=true
Geocoder based implementation:
private String getZipCodeFromLocation(Location location) {
Address addr = getAddressFromLocation(location);
return addr.getPostalCode() == null ? "" : addr.getPostalCode();
}
private Address getAddressFromLocation(Location location) {
Geocoder geocoder = new Geocoder(this);
Address address = new Address(Locale.getDefault());
try {
List<Address> addr = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addr.size() > 0) {
address = addr.get(0);
}
} catch (IOException e) {
e.printStackTrace();
}
return address;
}
You can get also other information from address, for example city name.