How to retrieve high quality compass orientation (as in Google Maps)?

前端 未结 4 1869
青春惊慌失措
青春惊慌失措 2021-02-09 07:53

All of the guides to getting compass orientation in Android I\'ve found have a bug: when you hold the phone in portrait mode and \"look\" above the horizon, the compass arrow tu

4条回答
  •  Happy的楠姐
    2021-02-09 08:23

    OK, i figured it out myself. first you need to calculate the bearing from the compass. then the Maps api-2 camera can be rotated.

    public void updateCamera(float bearing) {
                CameraPosition currentPlace = new CameraPosition.Builder()
                        .target(new LatLng(centerLatitude, centerLongitude))
                        .bearing(bearing).tilt(65.5f).zoom(18f).build();
                googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(currentPlace));
    
        }
    

    set SensorListener in your code and call this method in onSensorChanged event. i have added a tilt value so the map will rotate in 3D.

    [Reference:] [Android Maps v2 rotate mapView with compass1

    if you can know about more details http://www.techotopia.com/index.php/Working_with_the_Google_Maps_Android_API_in_Android_Studio

提交回复
热议问题