How to get latitude & longitude from address on Android? [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I find the latitude and longitude from address?
I want to get latitude and longitude of particular address . How can i do this ?

private void getFromLocation(String address)
{
double latitude= 0.0, longtitude= 0.0;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(address , 1);
if (addresses.size() > 0)
{
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
latitude=p.getLatitudeE6()/1E6;
longtitude=p.getLongitudeE6()/1E6;
}
}
catch(Exception ee)
{
}
}
}

You can get from the below code,
private void GetLatitudeAndLongitude() {
geocoder = new Geocoder(mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocationName(txtLocation.getText().toString().trim().toLowerCase(), 1);
if (addresses.size() > 0) {
homeInfoModel.setLalitude(String.valueOf(addresses.get(0).getLatitude()));
homeInfoModel.setLongitude(String.valueOf(addresses.get(0).getLongitude()));
}
} catch (IOException e) {
e.printStackTrace();
}
}

This will give you (loop through) all the matches of the address string.
If you just desire the first match, make sure address.size() is great than 0, then take address.get(0);
In my usage, I get all matches, and show them as options. The user then clicks one, and it selects that GPS location.
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses;
try {
addresses = geocoder.getFromLocationName("Example StreeT, UK, DNFE", 20);
for(int i = 0; i < addresses.size(); i++) { // MULTIPLE MATCHES
Address addr = addresses.get(i);
double latitude = addr.getLatitude();
double longitude = addr.getLongitude(); // DO SOMETHING WITH VALUES
}
}

Related

android geocoder : how to retrieve the name of the city in the local of the country of the city?

i use the android geocoder to retrieve the name of cities. however the name is returned in the local we use to construct the geocoder. is their any way to retrieve the name in the local of the city (exemple paris for paris in france or Москва for moscow in russia).
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());
}
else
{
// do your staff
}
try this you will get the complete address
private void getLocation(double lat, double lng) {
try {
Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
address.getAddressLine(0);
address.getAddressLine(1);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(address.getAddressLine(i)).append(" ");
}
Log.d(TAG, "strReturnedAddress : " + strReturnedAddress);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
You can use the Geocoder's getFromLocation method:
final LatLng lLatLng = new LatLng(0.0, 0.0);
final LatLng lMaxResults = 10;
final Geocoder lGeocoder = new Geocoder(this, Locale.ENGLISH);
final List<Address> lAddresses = lGeocoder.getFromLocation(lLatLng.latitude, lLatLng.longitude, lMaxResults);

How to get postal code using user current location in android

I am trying to get postal code, but I am unable to get zipcode(postalcode). I can able to get current city but when I try to get the zipcode it's giving me a null pointer exception.
Can anyone help me.
final Geocoder gcd = new Geocoder(getApplicationContext(),
Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) Log.d(addresses.get(0).getLocality()); // I can get city name here.
Log.d(addresses.get(0).getPostalCode();// here i am getting nullpoiter exception
Try to use android built-in Geocoder to get details from latitude and longitude without calling google location api as below :
Initialize Gecoder using Context :
final Geocoder gcd = new Geocoder(context);
Get Address as result from Lat-Long, Here (10) max result.
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 10);
Iterate to result get required location details :
for (Address address : addresses) {
if(address.getLocality()!=null && address.getPostalCode()!=null){
Log.d(address.getLocality());
Log.d(address.getPostalCode();
break;
}
}
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
Address address=null;
String addr="";
String zipcode="";
String city="";
String state="";
if (addresses != null && addresses.size() > 0){
addr=addresses.get(0).getAddressLine(0)+"," +addresses.get(0).getSubAdminArea();
city=addresses.get(0).getLocality();
state=addresses.get(0).getAdminArea();
for(int i=0 ;i<addresses.size();i++){
address = addresses.get(i);
if(address.getPostalCode()!=null){
zipcode=address.getPostalCode();
break;
}
}
I Used google webservice to get the zipcode.
The following is the google web service
http://maps.googleapis.com/maps/api/geocode/json?latlng=lattitude,longitude&sensor=true
here lattitude and longitude. so replace those values and you will get response and parse them and get postal code.
I have utility method to grab postcode which is pretty neat and works fine..
public static String getPostalCodeByCoordinates(Context context, double lat, double lon) throws IOException {
Geocoder mGeocoder = new Geocoder(context, Locale.getDefault());
String zipcode=null;
Address address=null;
if (mGeocoder != null) {
List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 5);
if (addresses != null && addresses.size() > 0) {
for (int i = 0; i < addresses.size(); i++) {
address = addresses.get(i);
if (address.getPostalCode() != null) {
zipcode = address.getPostalCode();
Log.d(TAG, "Postal code: " + address.getPostalCode());
break;
}
}
return zipcode;
}
}
return null;
}

Geopoint to address

i want to know how to transfer geopoint to address Example
double src_lat =30.022584 ;
double src_long = 31.343822;
srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6));
how can i get the address of this srcGeoPoint
Try something like this
Geocoder geoCoder = new Geocoder(
getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
srcGeoPoint.getLatitudeE6() / 1E6,
srcGeoPoint.getLongitudeE6() / 1E6, 1);
String add = "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();
i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
taken from: http://mobiforge.com/developing/story/using-google-maps-android
see geocoding and reverse geocoding about halfway down the page
you might also want to look at https://developers.google.com/maps/documentation/geocoding/

Using Geocoder to show user's location Address

I have this method to show the user's latitude and longitude on a map activity:
public void animateMap(Location location){
double lat = location.getLatitude();
double lng = location.getLongitude();
Toast.makeText(MyMapActivity.this,
"Sie sind an\n" + lat + "\n" + lng, Toast.LENGTH_SHORT)
.show();
GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mapController.animateTo(point, new Message());
mapOverlay.setPointToDraw(point);
}
How to implement the Geocoder on my method? So the Toast will display the location's address instead of the coordinates
you use
Geocoder myLocation = new Geocoder(context, Locale.ENGLISH);
List<Address> myList= myLocation.getFromLocation(lat, lng, 1);
Address add = myList.get(0);
String addressString = add.getAddressLine(0);
String country = add.getCountryName();
String city = add.getLocality();
The easiest implementation is by using the geocoder class:
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
geocoder.getFromLocation(lat, lng, 1);
List<Address> ls=new ArrayList();
ls= geocoder.getFromLocation(lat, lng, 1);
String myLocation = ls.get(0).getSubAdminArea();
You can check all the information returned by this class and choose which one fits you most. It contains from country names to landmarks name, neighbors postalcodes... almost anything you may need.
But keep in mind, if Google has no info about this location will return a null string!
So for you exapmple should be something like that:
public void animateMap(Location location){
double lat = location.getLatitude();
double lng = location.getLongitude();
String myLocation;
try{
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
geocoder.getFromLocation(lat, lng, 1);
List<Address> ls=new ArrayList();
ls= geocoder.getFromLocation(lat, lng, 1);
myLocation = ls.get(0).getSubAdminArea();
}catch(Exception e){
myLocation="Sorry, we have no information about this location";
}
Toast.makeText(MyMapActivity.this,
"Sie sind an\n" + myLocation , Toast.LENGTH_SHORT)
.show();
GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mapController.animateTo(point, new Message());
mapOverlay.setPointToDraw(point);
}
Get the list of Addresses at your location from Geocoder and check for a null or empty result:
Geocoder geocoder = new Geocoder(getApplicationContext,Locale.getDefault());
List<Address> address = geocoder.getFromLocation(lat, long, 1);
String myLocation = "";
if(address != null && !address.isEmpty())
{
myLocation = address.get(0).getSubAdminArea();
}

How to get latitude & longitude from given address on Android?

I want to get latitude and longitude of particular address . How can i do this ?
This is the sample solution to your question.
OR
List<Address> foundGeocode = null;
/* find the addresses by using getFromLocationName() method with the given address*/
foundGeocode = new Geocoder(this).getFromLocationName("address here", 1);
foundGeocode.get(0).getLatitude(); //getting latitude
foundGeocode.get(0).getLongitude();//getting longitude
For more details Geocoder
public List<Address> getFromLocationName (String locationName, int maxResults)
From a Geocoder object, you can call the getFromLocation
public List<Address> getFromLocation (double latitude, double longitude, int maxResults)
It will return a list of Address object that has a method getLocality
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());
for more detail
show the location in google map from address...will get the latitude and longitude using the address...
Geocoder coder = new Geocoder(this);
List<Address> address;
try {
address = coder.getFromLocationName(strAddress,5);
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
strAddress is string that you pass of address. address variable is address converting and getting address.

Categories

Resources