如何在每秒更新当前位置后停止放大谷歌地图?

时间:2014-04-13 10:31:38

标签: android zoom default google-maps-android-api-2

我开发了一款Android应用程序,每秒都可以获得用户当前的位置。但每次更新位置时,每次更新都会按2x放大,所以请帮我设置恒定的缩放级别,每次更新后不会缩放

//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();

1 个答案:

答案 0 :(得分:2)

您可以将animateCamera方法与CameraUpdateFactory一起使用,如下所示:

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

其中float 5.0f应该是您想要的缩放级别。 2.0f是最低变焦/最远,21.0f是最接近的。

因此,如果您没有更改缩放级别(例如,将其固定为5.0f)并且仅更新纬度和经度值,则可以获得所需的结果。