创建为您提供位置的按钮(Android Studio)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建为您提供位置的按钮(Android Studio)相关的知识,希望对你有一定的参考价值。
我想创建一个按钮,告诉我我的设备的确切位置,我将其指向MapView并给了我设备的坐标,我不知道如何将动作提供给Button
。我试图使用LocationListener,但它给了我一个错误
现在这是我的代码:
public class CameraFragment extends Fragment implements OnMapReadyCallback{
double latitude, longitude;
Button myplacebtn;
MapView mapView;
GoogleMap map;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.camera_fragment, container, false);
myplacebtn = (Button) view.findViewById(R.id.myplacebtn);//Button --get my location
mapView = (MapView) view.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
//get a MapView
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
// Setting a click event handler for the map
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
setCoordinates.setText(latLng.latitude + " , " + latLng.longitude);
latitude = latLng.latitude;
longitude = latLng.longitude;
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
// Clears the previously touched position
map.clear();
// Animating to the touched position
map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
map.addMarker(markerOptions);
}
});
map.getUiSettings().setMyLocationButtonEnabled(false);
LatLng jerusalem = new LatLng(32.1105435, 34.8683683);
CameraUpdate miLocation = CameraUpdateFactory.newLatLngZoom(jerusalem, 11);
map.moveCamera(CameraUpdateFactory.newLatLng(jerusalem));
googleMap.animateCamera(miLocation);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(jerusalem);
if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}, MY_REQUEST_INT);
}
return;
} else {
googleMap.setMyLocationEnabled(true);
}
googleMap.getUiSettings().setZoomControlsEnabled(true);
}
});
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
如果某人有一个例子,或某些迹象表明我可以开始编写代码,我会非常乐于助人。
答案
您可以使用FusedLocationProviderClient和位置更新请求。更多信息:https://developer.android.com/training/location/receive-location-updates
FusedLocationProviderClient locationProviderClient =
LocationServices.getFusedLocationProviderClient(this);
LocationRequest mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(TimeUnit.SECONDS.toMillis(10)) // 10 seconds
.setFastestInterval(TimeUnit.SECONDS.toMillis(1)); // 1 second
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
if (location != null) {
// your code here
}
}
}
};
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
locationProviderClient.requestLocationUpdates(mLocationRequest,
locationCallback, null);
}
另外,如果你想摆脱一些请求权限样板代码,我建议你试试RxPermissions库https://github.com/tbruyelle/RxPermissions
以上是关于创建为您提供位置的按钮(Android Studio)的主要内容,如果未能解决你的问题,请参考以下文章