Question

I have developed an android app which get users current location at every second. but while updating location every second it zoom in by 2x at every update at every time so please help me to set constant zoom level which will not zoom after updating every second

//code to get and send location information

          // create class obj

          gps = new GPSTracker(MainActivity.this);

                // check if GPS enabled
                    if (gps.canGetLocation()) {

                    latitude = gps.getLatitude();
                    longitude = gps.getLongitude();


                    // marker options
                    position = new LatLng(latitude, longitude);

                    // Instantiating MarkerOptions class
                    options = new MarkerOptions();

                    // Setting position for the MarkerOptions
                    options.position(position);

                    // Setting title for the MarkerOptions
                    options.title("Position");

                    // Setting snippet for the MarkerOptions
                    options.snippet("Latitude:" + latitude + ",Longitude:"
                                    + longitude);

                    // mapppppppppppp
                    try {
                        // Loading map
                        initilizeMap();

                        // moving to lat long location

                        // Adding Marker on the Google Map
                    googleMap.addMarker(options);

                // Creating CameraUpdate object for position
                CameraUpdate updatePosition = CameraUpdateFactory
                    .newLatLng(position);

                        // Creating CameraUpdate object for zoom
                CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(2);

            // 2 is zoomlevel on every update it is going zoom in and in
                    // latitude and longitude
                                      googleMap.moveCamera(updatePosition);

                        // Applying zoom to the marker position
                                googleMap.animateCamera(updateZoom);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                            // \n is for new line
                Toast.makeText(
                        getApplicationContext(),
                        "Your Location is - \nLat: " + crsp
                                + "\nLong: " + longitude,
                                    Toast.LENGTH_LONG).show();

                        } else {
                            // can't get location
                            // GPS or Network is not enabled
                            // Ask user to enable GPS/network in settings
                            gps.showSettingsAlert();
                        }

                        // new sendLocation().execute();
Was it helpful?

Solution

You could use the animateCamera method together with a CameraUpdateFactory, like this:

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                          new LatLng(latitude, longitude), 5.0f));

where the float 5.0f should be your desired zoom level. 2.0f is the lowest zoom / farthest away, 21.0f is the closest.

So if you are not changing the zoom level (keep it fixed at 5.0f, for example) and only updating the latitude and longitude values, you get the desired outcome.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top