I wanna draw a route on the basis of the entered source & destination address. I need to get lat/log on the basis of address to draw a map in android. So how can i get lat/log on the basis of entered address.
Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName("Loc",1);
String add = "";
if (addresses.size() > 0)
{
add = addresses.get(0);
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
Use the Google Geocoding API to get coordinates for an address: http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding
Related
I try get city according to current location with specific language i do that before like below and all work good but in my new project its not work!
Geocoder geo = new Geocoder(getApplicationContext(), new Locale("ja"));
List<Address> addresses = null;
try {
addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses.size() > 0) {
Log.d("CITY",addresses.get(0).getLocality());
city.setText(addresses.get(0).getLocality());
} else {
// do your stuff
}
} catch (IOException e) {
e.printStackTrace();
}
dos not matter which language be set always name of city return with English language!
after some research i understand must set geo.getFromLocation maxResults > 1 cause always first address returned in list , return with English language but the next items will be your specific language , so just need change
addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
to
addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 2);
and getLocality like this addresses.get(1).getLocality()
I want to show a continuous update of Lat and long along with location address .
I was able to receive continuous lat/lng but wasn't able to find the location address .
Basically , want to combine these two :
https://developer.android.com/training/location/receive-location-updates.html
https://developer.android.com/training/location/display-address.html
How can I achieve this?
Thanks.
Hi you can get a location address using Geocoder class
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
String city;
try {
addresses = gcd.getFromLocation(lat,lon, 1);
if (addresses.size() > 0)
city = addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
When I go maps|settings|edit home or work I see my home & work address, cool!
Anybody know how to get that same information from the Android SDK? I am working on a maps app and would like to plot those to points of reference, with out asking the user for them again?
Thanks
You will need to use reverse geocoding to convert the latitude/longitude into an address. More info about geocoding can be found here.
Here is an example:
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
List<Address> list = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
// sending back first address line and locality
result = address.getAddressLine(0) + ", " + address.getLocality();
}
} catch (IOException e) {
Log.e(TAG, "Impossible to connect to Geocoder", e);
}
By tapping on the map, How can I get the address of that location. On tap at particular location we can able listen and pass those co-ordinates we can get address but it should make work in android Google map v2.
If you have your LatLng data - it's quite easy. You need to use Goecoder.
protected String doInBackground(Location... params) {
Geocoder geocoder =
new Geocoder(mContext, Locale.getDefault());
// Get the current location from the input parameter list
Location loc = params[0];
// Create a list to contain the result address
List<Address> addresses = null;
try {
/*
* Return 1 address.
*/
addresses = geocoder.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
} catch (IOException e1) {
Log.e("LocationSampleActivity",
"IO Exception in getFromLocation()");
e1.printStackTrace();
return ("IO Exception trying to get address");
} catch (IllegalArgumentException e2) {
// Error message to post in the log
String errorString = "Illegal arguments " +
Double.toString(loc.getLatitude()) +
" , " +
Double.toString(loc.getLongitude()) +
" passed to address service";
Log.e("LocationSampleActivity", errorString);
e2.printStackTrace();
return errorString;
}
// If the reverse geocode returned an address
if (addresses != null && addresses.size() > 0) {
// Get the first address
Address address = addresses.get(0);
/*
* Format the first line of address (if available),
* city, and country name.
*/
String addressText = String.format(
"%s, %s, %s",
// If there's a street address, add it
address.getMaxAddressLineIndex() > 0 ?
address.getAddressLine(0) : "",
// Locality is usually a city
address.getLocality(),
// The country of the address
address.getCountryName());
// Return the text
return addressText;
} else {
return "No address found";
}
}
...
}
...
}
The best way to do it is to use AsyncTask. You will find complete example at Android Devlopers' site: http://developer.android.com/training/location/display-address.html
This may help you to use the complete map opertion
You can do this with the android GeoCoder. With GoogleMaps v2 you can use longpress to get a LatLng point. By taking those coordinates and passing them to the GeoCoder you can "reverse geocode" to find the address.
how to convert the latitude and longitude in to postal address. i am not possible to use mapview to get geopoint.I wanted to convert latitude and longitude in address without use of mapview
This is called Reverse Geocoding, there are number of services that you can use including Google, Yahoo and Bing
See this article for free Geocoding services
Android has GeoCoder class for this :
public String getAddress(double latitude, double longitude){
if (Geocoder.isPresent()) {
try {
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
if(addresses != null && addresses.size() > 0) {
Address first = addresses.get(0);
StringBuilder sb = new StringBuilder();
if(first.getMaxAddressLineIndex() > 0){
sb.append(first.getAddressLine(0) + ", ");
}
sb.append(first.getLocality() + ", ");
sb.append(first.getCountryName());
return sb.toString();
} catch (Exception e) {
Log.e("MyAPP", "Reverse geo lookup failed", e);
return "Unavailable"
}
} else {
return "unavailable";
}
}
Go to project properties > Android > set project build target to Google