2

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?

Nompumelelo
  • 929
  • 3
  • 17
  • 28
Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143

2 Answers2

15

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;
}
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
  • sorry, i havn't understood what you exactly want? can you explain it briefly or give eg. – Amol Sawant Mar 29 '13 at 11:01
  • I want that when the user starts dragging the marker and reaches the boundary of the map screen ( visible portion) then the map screen should slide autmatically to other side, so as to able to show more. – Gaurav Arora Mar 29 '13 at 12:03
  • try this [link](http://android-er.blogspot.in/2013/01/google-maps-android-api-v2-example_7.html) Hope this help you. – Amol Sawant Mar 30 '13 at 09:02
  • geocoder always gives me Timeout exception what to do now? – Shashwat Gupta Aug 30 '17 at 07:33
  • use this https://stackoverflow.com/questions/15711499/get-latitude-and-longitude-with-geocoder-and-android-google-maps-api-v2/15747491?noredirect=1#comment72135755_15747491 – Amol Sawant Oct 03 '17 at 09:53
4

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

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • Hi Ovidiu . Can you please help me in this matter, I want that when I drag the marker upto the boundaries, then the map should automatically scroll to the other direction. Is it possible ? - google maps api v2 ? – Gaurav Arora Mar 29 '13 at 10:12
  • 1
    maybe you should post another question for this. – Ovidiu Latcu Mar 29 '13 at 12:55