How to get the latitude and longitude of location where user taps on the map in android

前端 未结 4 773
后悔当初
后悔当初 2020-12-22 03:41

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 locatio

4条回答
  •  时光说笑
    2020-12-22 04:33

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

提交回复
热议问题