-1

I am able to display the latitude and longitude on moving marker. When this onMapReady calls, the latitude and longitude sets to zero.

Here is the code:

public void onMapReady(GoogleMap googleMap) {
    Log.d(TAG, "OnMapReady");
    mMap = googleMap;

    mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            Log.d("Camera postion change" + "", cameraPosition + "");
            mCenterLatLong = cameraPosition.target;


            mMap.clear();

            try {

                Location mLocation = new Location("");
                mLocation.setLatitude(mCenterLatLong.latitude);
                mLocation.setLongitude(mCenterLatLong.longitude);

                startIntentService(mLocation);
                mLocationMarkerText.setText("Lat : " + mCenterLatLong.latitude + "," + "Long : " + mCenterLatLong.longitude);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }

}

I would like to set -34, 151 lat and long when i opened the app or else i would like to get current location when the app is opened. How can I achieve this?

Jacky
  • 298
  • 3
  • 15

2 Answers2

1

You add this code in onMapReady(GoogleMap googleMap)

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(-34, 151))
.zoom(13)   //optional if you want to zoom or not
.build();
MapFragment.newInstance(new GoogleMapOptions()
.camera(cameraPosition));

And to get the current location on map check this answer

Mayank Bhatnagar
  • 1,316
  • 2
  • 13
  • 21
1

Add below line of code in onMapReady(GoogleMap googleMap)

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-34, 151), 12.0f));

Kuls
  • 2,047
  • 21
  • 39
  • How can i get the current location onMapReady? – Jacky Sep 28 '17 at 08:45
  • Check out the blog http://droidmentor.com/get-the-current-location-in-android/ – Kuls Sep 28 '17 at 08:52
  • 1
    If it helped you then please accept it so it could be useful for others as well. – Kuls Sep 28 '17 at 08:52
  • How can i retrieve lat and long from mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-34, 151), 12.0f));? – Jacky Sep 28 '17 at 08:57
  • You can not get LatLong from that code line. You need to pass the LatLong as a parameter and you will get focus of that particular LatLong position – Kuls Sep 28 '17 at 08:59