【问题标题】:Create Button that gives you your location (Android Studio)创建为您提供位置的按钮 (Android Studio)
【发布时间】:2018-09-11 08:25:52
【问题描述】:

我想创建一个按钮,告诉我我的设备的确切位置,我将它指向一个 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();
    }

}

如果有人有一个例子,或者一些指示我可以开始编写代码,我会非常有帮助。

【问题讨论】:

标签: android google-maps android-studio location


【解决方案1】:

您可以将 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

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 2015-02-22
    • 1970-01-01
    • 2017-07-01
    • 2016-05-13
    相关资源
    最近更新 更多