将minZoom与map.fitBounds结合使用不会出错

时间:2018-06-19 13:23:05

标签: leaflet mapbox

我有一个Leaflet地图,我试图使用map.fitbounds()绘制多个点,但我想设置一个minZoom,以便用户无法将其一直缩放到超过9的缩放级别。我目前使用请在下面的步骤中向地图添加图钉,除非有时将地图当成多个点需要在更大的缩放比例下适应地图,否则会导致地图失败。如果将地图适应范围设置为高于9,则可以正常工作,但低于9则不会绘制。

我想知道是否有人会既保护地图不因minZoom失败而失败,又不知道如何在某些情况下使用fitBounds并阻止用户如此远地缩放。

map.options.minZoom = 9;
map.options.maxZoom = 15;
var fg = L.featureGroup().addTo(map);
for ( var i=0; i < markersArray.length; ++i )
{
  var linkid = (markersArray[i]['linkid']);
  var linkurl = (markersArray[i]['linkurl']);
 L.marker( [markersArray[i].lat, markersArray[i].lng], {icon: myIcon} )
  .bindPopup( '<div id="mapPinDetails"><h3>' + markersArray[i].name + '</h3>' + markersArray[i].address + '<br /></div>' )
  .addTo(fg);
}
  map.fitBounds(fg.getBounds());

1 个答案:

答案 0 :(得分:0)

  

在9以下,它不会绘图

可以,但是由于您不允许地图缩小到9以下,因此地图保持在9缩放级别,并且您可能看不到外部标记。但是,如果平移,您可以看到它们。视图中心与使用较低的缩放比例时可以达到的视图中心相同:

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet-src.js" integrity="sha512-IkGU/uDhB9u9F8k+2OsA6XXoowIhOuQL1NTgNZHY1nkURnqEGlDZq3GsfmdJdKFe1k1zOc6YU2K7qY+hF9AodA==" crossorigin=""></script>

<div id="map" style="height: 180px"></div>
@Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        mMap.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView1(strImage,strNameplace)))
                    .position(lnglng));
         }

private Bitmap getMarkerBitmapFromView1(String strImage, String strNameplace) {

        final View customMarkerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.marker_custom, null);
        markerImageView = customMarkerView.findViewById(R.id.ivan);

        final Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                markerImageView.setImageBitmap(bitmap);
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        };

        markerImageView.setTag(target);

        Picasso.with(this)
                .load(strImage)
                .into(target);

        customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
        customMarkerView.buildDrawingCache();
        Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
        Drawable drawable = customMarkerView.getBackground();
        if (drawable != null)
            drawable.draw(canvas);
        customMarkerView.draw(canvas);
        return returnedBitmap;
    }

如果您需要进一步的帮助,则必须解释“ 它会导致地图失败”的含义,并尽可能分享一个可复制的示例。