Reputation: 8362
I am making a demo app with Google Maps Api V2.
I have added a simple marker and I have made it draggable
Here is my code:
onCreate method()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_v2);
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setOnMapClickListener(this);
googleMap.setOnMarkerDragListener(this);
googleMap.setOnMapLongClickListener(this);
set info method()
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
LayoutInflater inflater = (LayoutInflater) MapsDemo.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom_info, null);
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText(marker.getSnippet());
return v;
}
});
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
addMarker method
marker = googleMap.addMarker(new MarkerOptions()
.position(ROMA)
.title("Hello")
.snippet("Nice Place")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.draggable(true));
@Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDragEnd(Marker marker) {
LatLng field = marker.getPosition();
System.out.println("LatitudenLongitude:" + field.latitude + " " + field.longitude);
}
@Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMapLongClick(LatLng latlng) {
// TODO Auto-generated method stub
}
@Override
public void onMapClick(LatLng latlng) {
// TODO Auto-generated method stub
}
}
Now I want the address to come out when the user clicks on the marker.
Simple question is : How to get the address( name) from Lat Long in API v2?
Upvotes: 2
Views: 15159
Reputation: 14342
Try this:
public List<Address> getAddress() {
if (latitude != 0 && longitude != 0) {
try {
Geocoder geocoder = new Geocoder(context);
List<Address> 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);
Log.d("TAG", "address = " + address + ", city = " + city + ", country = " + country);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(context, "latitude and longitude are null", Toast.LENGTH_LONG).show();
}
return addresses;
}
Upvotes: 15
Reputation: 72341
You should use the Geocoder
available in the Android API
. You will need to make a call to getFromLocation(latitude,longitude,maxResults)
which will return a List<Address>
.
Upvotes: 4