How to get gps coordinates from center of map in android when moving map in real time

What I have: I have an image that is displayed in the center of the map.

What I am trying to do: When I move the map, I want to select the lat / long coordinates from the center of the map and show it in a toast that always changes when I move the map


MainActivity.java

public class MainActivity extends Activity {


    ImageView imageView1;
    // Google Map
    private GoogleMap googleMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView1=(ImageView) findViewById(R.id.imageView1);

        // Loading map
        initilizeMap();
        addMarker();
        setCamera();

    }


    private void setCamera() {
        //Fix the camera to the user location
        MarkerOptions marker = new MarkerOptions().position(new LatLng(12.952782, 77.636247)).title("Your Location");
        // HUE_RED color icon
        marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        // adding marker
        googleMap.addMarker(marker);
        CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(12.952782, 77.636247)).zoom(16).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }


    private void addMarker() {
        MarkerOptions marker = new MarkerOptions().position(new LatLng(12.952782, 77.636247));
        // HUE_AZURE color icon
        marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
        // adding marker
        googleMap.addMarker(marker);
    }


    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }


    /**
     * function to load map. If map is not created it will create it for you
     * @throws Exception 
     * */
    private void initilizeMap() {
        try {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

                //Map Explicit settings//
                //Map Rotate Gesture
                googleMap.getUiSettings().setRotateGesturesEnabled(true);
                //My Location Button
                googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                //Compass Functionality
                googleMap.getUiSettings().setCompassEnabled(true);
                //Zooming Functionality
                googleMap.getUiSettings().setZoomGesturesEnabled(true);
                //Zooming Buttons
                googleMap.getUiSettings().setZoomControlsEnabled(true);
                //Showing Current Location
                googleMap.setMyLocationEnabled(true);

                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(this,"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Exception e) {


        }
    }
}

      

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="center" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/marker" />

</FrameLayout>

      


change

googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

            @Override
            public void onCameraChange(CameraPosition arg0) {
                googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {
                        LatLng latLng=googleMap.getCameraPosition().target;
                        Log.d("TAG", latLng.toString());
                        //Toast.makeText(this, latLng.toString(), Toast.LENGTH_LONG).show();
                    }
                });
            }
        });

      

+3


source to share


1 answer


Firstly: -

 mMap.getCameraPosition().target

      

the above cut code will give you the center position of Google Maps. while loading the map do

googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
    Log.e("TAG", googleMap.getCameraPosition().target.toString());
}

      



});

to move the map: -

googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

        @Override
        public void onCameraChange(CameraPosition arg0) {
            googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {
                    LatLng latLng= arg0.target;
                    Log.d("TAG", latLng.toString());
                    //Toast.makeText(this, latLng.toString(), Toast.LENGTH_LONG).show();
                }
            });
        }
    });

      

I hope this helps you ..!

+1


source







All Articles