Google Maps API v2默认标记未与LocationCallback()中手动绘制的标记对齐

时间:2018-07-04 09:53:48

标签: android google-maps google-maps-api-2 mapfragment android-fusedlocation

过去两周,我一直在网上寻找有关此问题的信息,看来我没有问正确的问题,因此请在此处发布。预先感谢您提供任何信息。

使用Android v7.1和Google Maps API v2,我想在地图片段上显示用户的位置。我的问题是:为什么默认的当前位置标记(在下面的示例图中的蓝色圆圈)经常不与我手动绘制以显示用户位置的标记(绿色圆圈)对齐?该代码会在更改位置时重绘绿色标记,所以我试图弄清为什么默认默认位置标记移动时没有频繁重绘绿色标记。

我是否只需要更新LocationCallback()被呼叫的频率/位移?目前设置为500ms,位移为2米。如果是这样,会对电池产生重大影响吗?

我已经尝试过使用onLocationChanged()而不是融合位置api的LocationCallback(),但是如果用户没有互联网连接,我的手动标记就不会更新。

我意识到我的代码中有一些问题,但是我想我的理解不够好,我无法自己解决。再次感谢您提供任何见解。


相关代码和示例图片:

来自MainActivity:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
        mapFragment.getMapAsync(this);

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                super.onLocationResult(locationResult);
                mLastLocation = locationResult.getLastLocation();
                mapManager.updateLocation(mLastLocation); //this is where the green marker is drawn
            }
        };
    }

    @Override
    public void onConnectionSuspended(int i) {}

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}

    @Override
    protected void onResume() {

        super.onResume();

        mapState = new MapState(this);

        if (mapFragment == null) {
            mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);
            mapFragment.getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mapManager.mMap = googleMap;
        mapManager.mMap.setOnMapLongClickListener(this);
        mapState = new MapState(this);

        //Initialize Google Play Services
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
                mapManager.mMap.setMyLocationEnabled(true);
            }
        }
        else {
            buildGoogleApiClient();
            mapManager.mMap.setMyLocationEnabled(true);
        }

        CameraPosition position = mapState.getSavedCameraPosition();
        if (position != null) {
            CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
            mapManager.mMap.setMapType(mapState.getSavedMapType());
        }
    }

    protected synchronized void buildGoogleApiClient() {
        //https://stackoverflow.com/questions/37102240/cannot-resolve-symbol-mgoogleapiclient-android-studio
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }

    private void setupAndRequestLocationPermissions() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(500); 
        mLocationRequest.setFastestInterval(500);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        mLocationRequest.setSmallestDisplacement(2); //10 meters
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                    mLocationCallback,
                    null);
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        setupAndRequestLocationPermissions();
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // Permission was granted.
                    if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                            == PackageManager.PERMISSION_GRANTED) {
                        if (mGoogleApiClient == null) {
                            buildGoogleApiClient();
                        }
                        mapManager.mMap.setMyLocationEnabled(true);

                } else {
                    // Permission denied, Disable the functionality that depends on this permission.
                    Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                }
                return;
            }
        }
    }}

来自清单:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

示例图片:

example google map image

0 个答案:

没有答案