1

I am google map apis for my current location. I am working on map application in which it first load current position then user can be able to point to any where on the map. My problem is that if i am moving, then my location is again and again moving towards my current position and user is not able to point the desired location on map as it again routes towards its current location. My code is given below, in which i am using code to initialize the map. Your help will be appreciated.

private static final LocationRequest REQUEST = LocationRequest.create()
            .setInterval(8000) // 5 seconds
            .setFastestInterval(16) // 16ms = 60fps
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

private void setUpMapIfNeeded() {
        if (mMap == null) {

            mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
            mMap.clear();
            mMap.setOnMapClickListener(MyLocationActivity.this);

            if (mMap != null) {
                mMap.setOnMapClickListener(MyLocationActivity.this);
                mMap.setMyLocationEnabled(true);
                mMap.setBuildingsEnabled(true);
                mMap.getUiSettings().setCompassEnabled(false);
                mMap.setIndoorEnabled(true);
                mMap.setMapType(mMap.MAP_TYPE_NORMAL);
                mMap.setTrafficEnabled(true);
            }
        }
    }

    private void setUpLocationClientIfNeeded() {
        if (mLocationClient == null) {
            mLocationClient = new LocationClient(getApplicationContext(),
                    connectionCallbacks, onConnectionFailedListener);
        }
    }

    ConnectionCallbacks connectionCallbacks = new ConnectionCallbacks() {

        @Override
        public void onDisconnected() {

        }

        @Override
        public void onConnected(Bundle connectionHint) {
            showLoadingDialog();
            mLocationClient.requestLocationUpdates(REQUEST, locationListener);
        }
    };

    OnConnectionFailedListener onConnectionFailedListener = new OnConnectionFailedListener() {

        @Override
        public void onConnectionFailed(ConnectionResult result) {

        }
    };

    OnMyLocationChangeListener onMyLocationChangeListener = new OnMyLocationChangeListener() {

        @Override
        public void onMyLocationChange(Location location) {

        }
    };

    LocationListener locationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {

            dismissLoadingDialog();

            float diff = 0;

            if (lastLocation != null) {
                diff = location.distanceTo(lastLocation);
            }

            if ((lastLocation == null) || (diff > 5)) {

                LatLng latLng = new LatLng(location.getLatitude(),
                        location.getLongitude());

                CameraPosition cameraPosition = new CameraPosition(latLng, 14,
                        45, 0);

                CameraUpdate cameraUpdate = CameraUpdateFactory
                        .newCameraPosition(cameraPosition);

                mMap.animateCamera(cameraUpdate, 25, null);
            markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.flag));
                mMap.getUiSettings().setZoomControlsEnabled(true);
                mMap.getUiSettings().setZoomGesturesEnabled(true);
                lastLocation = location;
            }

        }
    };

1 Answers1

2

@using_coding there may be many ways to do it, i didn't check this code, but may be it will help you in your scenario.

1: Make a boolean checking in the creation of your activity like:

boolean isFirstTime = true;

In your location listener for the first time make:

mMap.setMyLocationEnabled(true);

And in this column make your isFirstTime to be false and in that block make MyLocationEnabled to be false. In this way if users location is changed then it will check boolean and if it is false then it will not update your current location.

2: Second way is not the solution but you can also do it, like you can set the location request time to be relatively more.

android_explorer
  • 345
  • 1
  • 3
  • 10