Я нашел этот кодКак я могу получить свое местоположение? [Android]
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));
}
Но getMyLocation() является depricated. Как я могу определить свое местоположение и сначала показать это на моей карте.
Мой код теперь:
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;
}
};
Я прочитал developers.android.com, но не нашел ответа. Мне нужно просто показать свое текущее местоположение на карте. Noghing more
Я видел много примеров здесь, на этом сайте. Everybode использовать getMyLocation() –
Мне понравилось здесь http://stackoverflow.com/a/16005386/6277732. меня устраивает! –