问题
I am developing an application wherein I get the latitude, longitude in my android device and post them to a web server.
and in a web application I show the location details with the help of google maps.
Here I have huge collection of lat long values which i loop it in my jsp and create multiple markers with info windows.
Now the problem is that I need to show the location name of the particular latitude and longitude in the Info Window of google maps.
Can anyone help me with the google maps script or how can i get the location name from the lat long values in my android phone itself, so that I can post that to my web server.
回答1:
Here i am given a single just pass the latitude and longitude in this function then you got all the information related to this latitude and longitude.
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
I hope you got you answer.
回答2:
I have a very simple answer to my own question posted couple of months back.... Please check this... It worked for me..
Geocoder myLocation = new Geocoder(yourContext, Locale.getDefault());
List<Address> myList = myLocation.getFromLocation(Double.parseDouble(latitude),Double.parseDouble(longitude), 1);
Address address = (Address) myList.get(0);
String addressStr = "";
addressStr += address.getAddressLine(0) + ", ";
addressStr += address.getAddressLine(1) + ", ";
addressStr += address.getAddressLine(2);
回答3:
Here is the code...
StringBuilder _homeAddress = null;
try{
_homeAddress = new StringBuilder();
Address address = null;
List<Address> addresses = _coder.getFromLocation(_lat,_lon,1);
for(int index=0; index<addresses.size(); ++index)
{
address = addresses.get(index);
_homeAddress.append("Name: " + address.getLocality() + "\n");
_homeAddress.append("Sub-Admin Ares: " + address.getSubAdminArea() + "\n");
_homeAddress.append("Admin Area: " + address.getAdminArea() + "\n");
_homeAddress.append("Country: " + address.getCountryName() + "\n");
_homeAddress.append("Country Code: " + address.getCountryCode() + "\n");
_homeAddress.append("Latitude: " + address.getLatitude() + "\n");
_homeAddress.append("Longitude: " + address.getLongitude() + "\n\n");
}
}
catch(Exception e){
}
回答4:
Use android.location.geocoder.getFromLocation(double latitude, double longitude, int maxResults)
it will give you a list of addresses.
回答5:
Its Working for me:
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(DistaceCalculating.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
String currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
double latitude = obj.getLatitude();
double longitude = obj.getLongitude();
String currentCity= obj.getSubAdminArea();
String currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
System.out.println("obj.getCountryName()"+obj.getCountryName());
System.out.println("obj.getCountryCode()"+obj.getCountryCode());
System.out.println("obj.getAdminArea()"+obj.getAdminArea());
System.out.println("obj.getPostalCode()"+obj.getPostalCode());
System.out.println("obj.getSubAdminArea()"+obj.getSubAdminArea());
System.out.println("obj.getLocality()"+obj.getLocality());
System.out.println("obj.getSubThoroughfare()"+obj.getSubThoroughfare());
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
回答6:
Here you Go!
private String getAddress() {
GPSTracker gps = new GPSTracker(ProductList.this);
Geocoder geocoder = new Geocoder(ProductList.this, Locale.getDefault());
String address="";
try {
List<Address> addresses = geocoder.getFromLocation(gps.getLatitude(), gps.getLongitude(), 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
//optional
/* add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();*/
Log.e("Location", "Address" + add);
address=add;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return address;
}
来源:https://stackoverflow.com/questions/6172451/given-a-latitude-and-longitude-get-the-location-name