I get address from location as:
GeoPoint p;
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
Location location = new Location("A");
location.setLatitude(34.7461307);
location.setLongitude(135.5738767);
p = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
List<Address> add = null;
try {
add = geocoder.getFromLocation(p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
String addressjian = null;
if (add != null && add.size() > 0) {
// Get address
String post = "";
for (int i = 0; i <= add.get(0).getMaxAddressLineIndex(); i++) {
String country = add.get(0).getCountryName();
post = add.get(0).getPostalCode();
if (!add.get(0).getAddressLine(i).equals(country)) {
if (addressjian != null && !addressjian.equals("")) {
addressjian = addressjian + ", ";
}
addressjian += add.get(0).getAddressLine(i);
}
}
}
If I set language as English, the result is:
[Address[addressLines=[0:"1丁目-21 Yagumo Higashimachi",1:"Moriguchi, Osaka Prefecture 570-8588",2:"Japan"],feature=570-8588,admin=null,sub-admin=null,locality=null,thoroughfare=null,postalCode=570-8588,countryCode=JP,countryName=Japan,hasLatitude=true,latitude=34.7456387,hasLongitude=true,longitude=135.5744525,phone=null,url=null,extras=null]]
But If I set language to Japanese, the result is:
[Address[addressLines=[0:"〒570-8588",1:"日本"],feature=570-8588,admin=null,sub-admin=null,locality=null,thoroughfare=null,postalCode=570-8588,countryCode=JP,countryName=日本,hasLatitude=true,latitude=34.7456387,hasLongitude=true,longitude=135.5744525,phone=null,url=null,extras=null]]
Why it doesn't get name of the street? Why does it depend with language of device?
Related
Am going to share a solution which include save and retrieve location inside a JPEG image file.The latitude and longitude is save and retrive inside the image metadata using ExifInterface
More about ExifInterface can be found here
http://developer.android.com/reference/android/media/ExifInterface.html
public void saveLocation() {
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitude);
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitude);
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
}
public void retriveLocation() {
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
String[] latitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE).split(",");
String[] longitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE).split(",");
String[] tmp = new String[2];
tmp = latitudeValue[0].split("/");
setLatitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
tmp = longitudeValue[0].split("/");
setLongitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
} catch (IOException e) {
e.printStackTrace();
}
}
Using ExifInterface you can get following information from media.
Variable Declaration.
String mediaDateTime,attrLATITUDE,attrLATITUDE_REF,attrLONGITUDE,attrLONGITUDE_REF,zip, city,state, country;;
Double Latitude, Longitude;
List<Address> addresses;
Geocoder geocoder;
User ExifInterface using following way.
ExifInterface exifInterfaceMedia = new ExifInterface(<Your Image Path>);
// This will give you data and time
mediaDateTime = exifInterfaceMedia.getAttribute(ExifInterface.TAG_DATETIME);
attrLATITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
attrLATITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
attrLONGITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
attrLONGITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null) && (attrLONGITUDE_REF != null)) {
valid = true;
if (attrLATITUDE_REF.equals("N")) {
Latitude = convertToDegree(attrLATITUDE);
} else {
Latitude = 0 - convertToDegree(attrLATITUDE);
}
if (attrLONGITUDE_REF.equals("E")) {
Longitude = convertToDegree(attrLONGITUDE);
} else {
Longitude = 0 - convertToDegree(attrLONGITUDE);
}
try {
addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
zip = addresses.get(0).getPostalCode();
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
if (zip != null) {
title += zip + ",";
}
if (city != null) {
title += city + ",";
}
if (state != null) {
title += state + ",";
}
if (country != null) {
title += country;
}
} else {
title = "Unknown Location";
}
}
private Double convertToDegree(String stringDMS) {
Double result = null;
String[] DMS = stringDMS.split(",", 3);
String[] stringD = DMS[0].split("/", 2);
Double D0 = new Double(stringD[0]);
Double D1 = new Double(stringD[1]);
Double FloatD = D0 / D1;
String[] stringM = DMS[1].split("/", 2);
Double M0 = new Double(stringM[0]);
Double M1 = new Double(stringM[1]);
Double FloatM = M0 / M1;
String[] stringS = DMS[2].split("/", 2);
Double S0 = new Double(stringS[0]);
Double S1 = new Double(stringS[1]);
Double FloatS = S0 / S1;
result = new Double(FloatD + (FloatM / 60) + (FloatS / 3600));
return result;
}
In my app I am using osm map. I have latitude and longitude.
using this method
proj = mapView.getProjection();
loc = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
String longitude = Double
.toString(((double) loc.getLongitudeE6()) / 1000000);
String latitude = Double
.toString(((double) loc.getLatitudeE6()) / 1000000);
Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "
+ longitude + " Latitude: " + latitude, Toast.LENGTH_SHORT);
toast.show();
So from here how I will query to get the city name from osm database. Please help me.
How can I convert this into human understandable form. Here is my code which I am using.link
Try this code for getting address.
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
for openstreammap
final String requestString = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" +
Double.toString(lat) + "&lon=" + Double.toString(lon) + "&zoom=18&addressdetails=1";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(requestString));
try {
#SuppressWarnings("unused")
Request request = builder.sendRequest(null, new RequestCallback() {
#Override
public void onResponseReceived(Request request, Response response) {
if (response.getStatusCode() == 200) {
String city = "";
try {
JSONValue json = JSONParser.parseStrict(response);
JSONObject address = json.isObject().get("address").isObject();
final String quotes = "^\"|\"$";
if (address.get("city") != null) {
city = address.get("city").toString().replaceAll(quotes, "");
} else if (address.get("village") != null) {
city = address.get("village").toString().replaceAll(quotes, "");
}
} catch (Exception e) {
}
}
}
});
} catch (Exception e) {
}
here is my solution. i think it works for you also.
public String ConvertPointToLocation(GeoPoint point) {
String address = "";
Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
if (addresses.size() > 0) {
for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
address += addresses.get(0).getAddressLine(index) + " ";
}
Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
return address;
}
I reffred many questions from Stack overflow and implemented the above procedure. But I am unable to get the adress. Please let me know If i missed something.. ?
myLoc = (TextView) findViewById(R.id.id1);
Geocoder geocoder = new Geocoder(getBaseContext(),Locale.getDefault());
try {
address = geocoder.getFromLocation(latitude, longitude, 1);
if (address.size() > 0) {
for (int i = 0; i < address.get(0)
.getMaxAddressLineIndex(); i++) {
display = "";
display += address.get(0).getAddressLine(i)
+ "\n";
}
}
} catch (Exception e2) {
// TODO: handle exception
}
myLoc.setText("Current Location:"+display);
System.out.println(display);
You can use Reverse geo coding to and Google apis to get address from latitude and longitude.
Reverse Geo Coding:
double currentLatitude;
double currentLongitude;
void getAddress(){
try{
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses =
gcd.getFromLocation(currentLatitude, currentLongitude,100);
if (addresses.size() > 0) {
StringBuilder result = new StringBuilder();
for(int i = 0; i < addresses.size(); i++){
Address address = addresses.get(i);
int maxIndex = address.getMaxAddressLineIndex();
for (int x = 0; x <= maxIndex; x++ ){
result.append(address.getAddressLine(x));
result.append(",");
}
result.append(address.getLocality());
result.append(",");
result.append(address.getPostalCode());
result.append("\n\n");
}
addressText.setText(result.toString());
}
}
catch(IOException ex){
addressText.setText(ex.getMessage().toString());
}
}
Google API: See this api which retrun address from latitude and longitude
http://maps.googleapis.com/maps/api/geocode/json?latlng=17.734884,83.299507&sensor=true
To know more read this
getMaxAddressLineIndex() returns an index which start from zero and thus your for-loop condition should be 0 <= maxIndex instead of 0 < maxIndex
You overwrite previous address lines on every iteration by assigning display = ""; and thus will end up with the last address line only. Is that on purpose?
Another good idea is to implement the LocationListener interface and register your Activity as a listener using LocationManager requestLocationUpdates() method. You can then override onLocationUpdate() to be informed whenever the location of the device changes. You provide the requestLocationUpdates() method the minimum amount of time that must pass before you will accept another update and how far the device must move before you get an update.
You can do like this to get complete address. In case you want country name
, state etc seperately .Then, I will not prefer you this method .
public class ParentHomeActivity extends AppCompatActivity {
...
private Geocoder geocoder;
private TextView mAddressTxtVu;
...
// I assume that you got latitude and longitude correctly
mLatitude = 20.23232
mLongitude = 32.999
String errorMessage = "";
geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
mlattitude,
mlongitude,
1);
} catch (IOException e) {
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, e);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " + "Latitude = " + mlattitude +",
Longitude = " + mlongitude, illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
// Log.i(TAG, getString(R.string.address_found));
mAddressTxtVu.setText(TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
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/
Im trying to display the address from an EditText using geoCoder and also it would be able editable to click on the finder button and update the googlemap display on the top of the screen, this is my code:
Geocoder geocoder = null;
MapView mapView = null;
int lat, lng;
EditText loc;
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.geoMap);
try
{
loc = (EditText)findViewById(R.id.location);
String locationName = loc.getText().toString();
List<Address> addressList = geocoder.getFromLocationName(locationName, 5);
if (addressList != null && addressList.size() > 0) {
lat = (int) addressList.get(0).getLatitude() * 1000000;
lng = (int) addressList.get(0).getLongitude() * 1000000;
GeoPoint pt = new GeoPoint(lat, lng);
mapView.getController().setZoom(10);
mapView.getController().setCenter(pt);
mapView.getController().animateTo(pt);
}
}
catch
(IOException e)
{
e.printStackTrace();
}
//
Button geoBtn = (Button) findViewById(R.id.geocodeBtn);
geocoder = new Geocoder(this);
//
geoBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try {
EditText loc = (EditText) findViewById(R.id.location);
String locationName = loc.getText().toString();
List<Address> addressList = geocoder.getFromLocationName(locationName, 5);
if (addressList != null && addressList.size() > 0) {
int lat = (int) addressList.get(0).getLatitude() * 1000000;
int lng = (int) addressList.get(0).getLongitude() * 1000000;
GeoPoint pt = new GeoPoint(lat, lng);
mapView.getController().setZoom(10);
mapView.getController().setCenter(pt);
mapView.getController().animateTo(pt);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
But it crashes at this point: List addressList = geocoder.getFromLocationName(locationName, 5);
Im using googles APi 2.3.3.