Setting max zoom level in google maps android api v2

后端 未结 16 2127
无人共我
无人共我 2020-12-02 17:14

I\'m currently working on developing apps by using Google maps android API v2. My code is as follows. Suppose map has several markers and zoom up to show all markers in disp

相关标签:
16条回答
  • 2020-12-02 17:28

    A recent update to the Google Maps API introduces the functions you require:

    GoogleMap.setMaxZoomPreference()
    
    GoogleMap.setMinZoomPreference()
    

    It still does not prevent the animation from playing, though.

    0 讨论(0)
  • 2020-12-02 17:29

    What I ended up doing is creating my own buttons and disabling the default ones so I would have full control.

    Something like this in the layout to place the buttons:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="bottom"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true">
    
        <Button
            android:id="@+id/bzoomin"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:gravity="center"
            android:textSize="28sp"
            android:text="+" />
        <Button
                android:id="@+id/bzoomout"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:gravity="center"
                android:textSize="23sp"
                android:text="—" />
    </LinearLayout>
    

    Then this in the code to disable the default buttons and setup our new ones:

    map.getUiSettings().setZoomControlsEnabled(false);
    
    // setup zoom control buttons
    Button zoomout = (Button) findViewById(R.id.bzoomout);
    zoomout.setOnClickListener(new OnClickListener(){
    
        @Override
        public void onClick(View v) {
            if(map.getCameraPosition().zoom >= 8.0f){
                // Zoom like normal
                map.animateCamera(CameraUpdateFactory.zoomOut());
            }else{
                // Do whatever you want if user went too far
                Messages.toast_short(MapsActivity.this, "Maximum zoom out level reached");
            }
        }
    
    });
    
    Button zoomin = (Button) findViewById(R.id.bzoomin);
    zoomin.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            if (map.getCameraPosition().zoom <= 14.0f) {
                // Zoom like normal
                map.animateCamera(CameraUpdateFactory.zoomIn());
            } else {
                // Do whatever you want if user went too far
                Messages.toast_short(MapsActivity.this, "Maximum zoom in level reached");
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-02 17:30

    With com.google.android.gms:play-services-maps:9.4.0 you can easily set min/max zoom. With GoogleMap.setMinZoomPreference() and GoogleMap.setMaxZoomPreference() you can set a prefered minimum and/or maximum zoom level. More see here.

    0 讨论(0)
  • 2020-12-02 17:31
    map.setMinZoomPreference(floatValue);
    
    0 讨论(0)
  • 2020-12-02 17:32

    Same as Arvis solution but using Location's [distanceBetween()](http://developer.android.com/reference/android/location/Location.html#distanceBetween(double, double, double, double, float[])) method to calculate distance between to points:

    @Override
    public void zoomToMarkers(Set<Marker> markers) {
    
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (Marker marker : markers) {
            builder.include(marker.getPosition());
        }
        LatLngBounds bounds = builder.build();
    
        // Calculate distance between northeast and southwest
        float[] results = new float[1];
        android.location.Location.distanceBetween(bounds.northeast.latitude, bounds.northeast.longitude,
                bounds.southwest.latitude, bounds.southwest.longitude, results);
    
        CameraUpdate cu = null;
        if (results[0] < 1000) { // distance is less than 1 km -> set to zoom level 15
            cu = CameraUpdateFactory.newLatLngZoom(bounds.getCenter(), 15);
        } else {
            int padding = 50; // offset from edges of the map in pixels
            cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
        }
        if (cu != null) {
            mMap.moveCamera(cu);
        }
    }
    
    0 讨论(0)
  • 2020-12-02 17:32

    Im not sure if this will work but can you try this

    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题