0

I found this code

    private void centerMapOnMyLocation() {

    map.setMyLocationEnabled(true);

    location = map.getMyLocation();

    if (location != null) {
        myLocation = new LatLng(location.getLatitude(),
                location.getLongitude());
    }
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation,
            Constants.MAP_ZOOM));
}

But getMyLocation() is depricated. How i can determine my location and show this on my map at first.

My code now is:

public class FragmentMap extends Fragment implements OnMapReadyCallback {

    MapView mapView;
    GoogleMap map;
    TextView t1, t2;
    private Marker marker;

    public FragmentMap() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_map, container, false);
        t1 = (TextView) v.findViewById(R.id.textView);
        t2 = (TextView) v.findViewById(R.id.textView2);
        mapView = (MapView) v.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(this); 

        return v;
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.getUiSettings().setZoomControlsEnabled(true);
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.setMyLocationEnabled(true);
        map.setOnMyLocationButtonClickListener(myLocationButtonClickListener);

    }



    private GoogleMap.OnMyLocationButtonClickListener myLocationButtonClickListener = new GoogleMap.OnMyLocationButtonClickListener() {
        @Override
        public boolean onMyLocationButtonClick() {
            Location location = map.getMyLocation();
            LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
            t1.setText(valueOf(location.getLatitude()));
            t2.setText(valueOf(location.getLongitude()));
            marker = map.addMarker(new MarkerOptions().position(loc));
            if(map != null){
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
            }
            return false;
        }
    };

I read developers.android.com but not found the answer. I need just show my current location on the map. Noghing more

Evgeny GooDi
  • 159
  • 2
  • 3
  • 12

2 Answers2

0

The below lines will gives you current lat and lng.

        double current_lat = mLastLocation.getLatitude();
        double current_longi = mLastLocation.getLongitude();


     it worked for me to get current location.Please follow this link for more details.

https://github.com/abhishekujjain/BasicMapsDemo

Justcurious
  • 2,201
  • 1
  • 21
  • 36
0

This tutorial helps you to learn location based service in android. You can choose between two types of location providers, GPS and NETWORK location providers.

Here are the steps to get location in Android:

  1. Provide permissions in manifest file for receiving location update

  2. Create LocationManager instance as reference to the location service

  3. Request location from LocationManager

  4. Receive location update from LocationListener on change of location

For more information and example code just follow the steps on the tutorial.

Check also this SO question, that I think it can help you.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31