Geopoint to address - android

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/

Related

Android map search query 'Orchard Road' returns address from Singapore instead of UK

Not sure what I need to correct to restrict all results from UK..
Presently query returns results from Singapore instead of UK.
I am using API 19.
try {
Geocoder geocoder = new Geocoder(getApplicationContext(),
Locale.UK);
List<Address> results = geocoder.getFromLocationName(toSearch,
1);
if (results.size() == 0) {
return null;
}
address = results.get(0);
GeoPoint p = new GeoPoint((int) (address.getLatitude() * 1E6),
(int) (address.getLongitude() * 1E6));
MapsActivity.this.myMapController.setCenter(p);
List<Overlay> mapOverlays = myMapView.getOverlays();
MapOverlay itemizedoverlay = (MapOverlay) mapOverlays.get(0);
itemizedoverlay.update(p);
} catch (Exception e) {
return null;
}

Why my app doesn't get address after change in language?

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?

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

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
}
}

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();
}

Show my position using a marker on android (previous marker still appear when position update)

I tried to show my current position on map using a marker, find my code below.
but if my position is changed (position updated), the previous marker is still appear.
how to remove previous marker. please help
public void UpdateMyPosition (Location location){
String addressString = "No location found";
if (location != null) {
// Update the map location.
double latitude = location.getLatitude();
double longitude = location.getLongitude();
GeoPoint geoPoint = new GeoPoint((int) (latitude * 1E6),(int) (longitude * 1E6));
mapController.animateTo(geoPoint);
Drawable drawable = this.getResources().getDrawable(R.drawable.red);
MapsOverlay itemizedoverlay2 = new MapsOverlay(drawable, this);
List<Overlay> myOverlays = mapView.getOverlays();
OverlayItem overlayitem2 = new OverlayItem(geoPoint, "", "");
itemizedoverlay2.addOverlay(overlayitem2);
myOverlays.add(itemizedoverlay2);
mapView.postInvalidate();
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 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));
}
addressString = sb.toString();
} catch (IOException e) {}
} else {
addressString = "No location found";
}
Toast.makeText(getBaseContext(),addressString, Toast.LENGTH_SHORT).show();
}
Put
myOverlays.clear();
before
myOverlays.add(itemizedoverlay2);
Maybe it'll be better to use bundled MyLocationOverlay, in this case drawing and updating will be performed automatically.
An other idea would be to not remove the old marker, but to change its position instead.

Categories

Resources