我發現這個代碼我如何獲得我的位置? [機器人]
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更多
我在這裏看到很多例子,這個網站。 Everybode使用getMyLocation() –
我不喜歡這裏http://stackoverflow.com/a/16005386/6277732。這個對我有用! –