1

In my Android application, I'm using google maps v2 to show map by getting the latitute and longitude of the device's current location And I'm showing pin on that location.

Now when user clicks or taps on any other location on the map, then I have to get that points latitude and longitude and i have to set the pin at that location.

Could you please tell me how get the latitude and longitude of the user taps/clicks location.

BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Suresh Basina
  • 185
  • 1
  • 6
  • 14

4 Answers4

4

An example of what i use. Change it accordingly for your needs. I use it with long press.

map.setOnMapLongClickListener(new OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng point) {
                    map.addMarker(new MarkerOptions().position(point).title("Custom location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));enter code here
            }
        });

the LatLng point contains the coordinated of the longpress

  • Hi Antonis, thanks for your reply. What is eventLocationCoords here. – Suresh Basina Jun 19 '14 at 09:02
  • I updated my answer. I removed it. It was a hashmap where i saved that location inside, and if the length of my hashmap was greater or equal to 1 then i didnt remove any other point. You dont need all these. I left only the line you need – Antonis Lambrianides Jun 19 '14 at 09:34
  • "the LatLng point contains the coordinated of the longpress" Do you mean "... the coordinates of the longpress"? – Neo42 Aug 10 '17 at 20:13
1

Try to use google-maps v2 built-in method.

map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
      @Override
      public void onMapClick(LatLng position) {
         Toast.makeText(context,position.latitude+" : "+position.longitude,Toast.LENGTH_SHORT).show();
      }
});
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
Haresh Chhelana
  • 23,930
  • 5
  • 51
  • 66
0

Try the following.

Write a class which derives from the Overlay class and override the onTap() method. Then you can add your overlay to the your MapView. A GeoPoint object, which represents the position of you tap, is passed to the onTap() method when you tab somewhere on the map.

OR

The modern answer here, using Android Maps v2, is to use OnMapClickListener, which gives you the LatLng of a tap on the map.

Shailendra Madda
  • 16,925
  • 14
  • 71
  • 115
0
// Setting onclick event listener for the map
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {

        // Creating MarkerOptions
        MarkerOptions options = new MarkerOptions();

        // Setting the position of the marker
       options.position(point);

        //Get LatLng from touched point

        double touchLat=point.latitude;
        double touchLong=point.longitude;

        ///here is reverse GeoCoding which helps in getting address from latlng

        try {
            Geocoder geo = new Geocoder(MainActivity.this.getApplicationContext(), Locale.getDefault());
            List<Address> addresses = geo.getFromLocation(touchLat,touchLong, 1);
            if (addresses.isEmpty()) {
                Toast.makeText(getApplicationContext(),"Waiting for Location",Toast.LENGTH_SHORT).show();
            }
            else {

                if (addresses.size() > 0) {
                    address =addresses.get(0).getFeatureName()
                            + ", " + addresses.get(0).getLocality()
                            + ", " + addresses.get(0).getAdminArea()
                            + ", " + addresses.get(0).getCountryName();
                    Toast.makeText(getApplicationContext(), "Address:- " +address, Toast.LENGTH_LONG).show();
                }

                // draws the marker at the currently touched location
                drawMarker(point,"Your Touched Position",address+"");

            }
        }
        catch (Exception e) {
            e.printStackTrace(); // getFromLocation() may sometimes fail
        }
  • 1
    Please consider adding an explanation to your answer, as well as a link to the documentation. – nvisser Mar 04 '17 at 20:17