一个地图中放多个标签,如何获取一个恰当的缩放级别
今天需要实现一个逻辑,需要在地图中放多个自定义对象,然后需要根据这些点,获取一个合适的缩放级别,可用的函数有这两个 :
自动调整到一个合适的缩放级别:
MapController::zoomToSpan();
获取一个缩放级别:
MapView::getZoomToBound();
不过有一点要注意,调用这两个函数,都要等到地图加载完成之后,即
MKMapViewListener::onMapLoadFinish() 被调用之后,不然会没有效果。
笔记先做成这样,以后有时间再总结!
代码如下:
package mars.maptest;
import java.util.ArrayList;
import android.util.Log;
import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Toast;
import android.graphics.Matrix;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.SystemClock;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MKMapViewListener;
/*
* 1. 不知道为什么,如果同时用getZoomToBound()和animateTo()两个函数,一定要把animateTo()放到getZoomToBound()后面,不然会无效.
* 2. getZoomToBound()函数的参数,应该是两个对角点,一定不要搞错.
* 3. zoomToSpan()函数的参数是 经纬度差,一定不要弄错.
* 4. 用2、3两个函数,一定要等到地图初始化完成,即 MKMapViewListener.onMapLoadFinish() 调用完之后再进行调用才能有效.
*/
public class MainActivity extends Activity{
private BMapManager mBMapMan = null;
private MapView mMapView = null;
private MapController mMapController = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBMapMan=new BMapManager(getApplication());
mBMapMan.init(null);
setContentView(R.layout.main);
mMapView=(MapView)findViewById(R.id.bmapsView);
mMapView.setBuiltInZoomControls(true);
//设置启用内置的缩放控件
mMapController=mMapView.getController();
mMapView.regMapViewListener(mBMapMan, mapViewListener);
}
@Override
protected void onDestroy(){
mMapView.destroy();
if(mBMapMan!=null){
mBMapMan.destroy();
mBMapMan=null;
}
super.onDestroy();
}
@Override
protected void onPause(){
mMapView.onPause();
if(mBMapMan!=null){
mBMapMan.stop();
}
super.onPause();
}
@Override
protected void onResume(){
mMapView.onResume();
if(mBMapMan!=null){
mBMapMan.start();
}
super.onResume();
}
MKMapViewListener mapViewListener = new MKMapViewListener() {
@Override
public void onMapMoveFinish() {
}
@Override
public void onClickMapPoi(MapPoi arg0) {
}
@Override
public void onGetCurrentMap(Bitmap arg0) {
}
@Override
public void onMapAnimationFinish() {
}
@Override
public void onMapLoadFinish() {
Log.i("vvcar","onMapLoadFinish()!");
drawMulCar(mMapView);
}
};
public void drawMulCar(View v){
// 116.413251,39.911752
// 116.306317,39.965746
// 116.658165,40.136296
// 116.683462,39.542834
// 117.184788,39.13925
// 119.613231,39.958225
double mLat1 = 39.911752;
double mLon1 = 116.413251;
double mLat2 = 39.965746;
double mLon2 = 116.306317;
double mLat3 = 40.136296;
double mLon3 = 116.658165;
double mLat4 = 39.542834;
double mLon4 = 116.683462;
double mLat5 = 39.13925;
double mLon5 = 117.184788;
double mLat6 = 39.958225;
double mLon6 = 119.613231;
// 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
GeoPoint p1 = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));
GeoPoint p2 = new GeoPoint((int) (mLat2 * 1E6), (int) (mLon2 * 1E6));
GeoPoint p3 = new GeoPoint((int) (mLat3 * 1E6), (int) (mLon3 * 1E6));
GeoPoint p4 = new GeoPoint((int) (mLat4 * 1E6), (int) (mLon4 * 1E6));
GeoPoint p5 = new GeoPoint((int) (mLat5 * 1E6), (int) (mLon5 * 1E6));
GeoPoint p6 = new GeoPoint((int) (mLat6 * 1E6), (int) (mLon6 * 1E6));
ArrayList<GeoPoint> gpList = new ArrayList<GeoPoint>();
gpList.clear();
gpList.add(p1);
gpList.add(p2);
gpList.add(p3);
gpList.add(p4);
gpList.add(p5);
gpList.add(p6);
//准备overlay图像数据,根据实情情况修复
Drawable mark3= getResources().getDrawable(R.drawable.car_gray);
//旋转图片
BitmapDrawable mark1 = rotateBitmap(R.drawable.car_gray,180);
BitmapDrawable mark2 = rotateBitmap(R.drawable.car_gray,45);
//用OverlayItem准备Overlay数据
OverlayItem item1 = new OverlayItem(p1,"item1","item1");
item1.setMarker(mark1);
OverlayItem item2 = new OverlayItem(p2,"item2","item2");
item2.setMarker(mark1);
OverlayItem item3 = new OverlayItem(p3,"item3","item3");
item3.setMarker(mark1);
OverlayItem item4 = new OverlayItem(p4,"item4","item4");
item4.setMarker(mark1);
OverlayItem item5 = new OverlayItem(p5,"item5","item5");
item5.setMarker(mark1);
OverlayItem item6 = new OverlayItem(p6,"item6","item6");
item6.setMarker(mark1);
//创建IteminizedOverlay
OverlayTest itemOverlay = new OverlayTest(mark1, mMapView);
//将IteminizedOverlay添加到MapView中
mMapView.getOverlays().clear();
mMapView.getOverlays().add(itemOverlay);
itemOverlay.addItem(item1);
itemOverlay.addItem(item2);
itemOverlay.addItem(item3);
itemOverlay.addItem(item4);
itemOverlay.addItem(item5);
itemOverlay.addItem(item6);
GeoPoint pMid = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));
GeoPoint pMax = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));
GeoPoint pMin = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));
getCenterAndEdg(gpList,pMid,pMax,pMin);
Log.i("vvcar","mid point is : " + String.valueOf( pMid.getLongitudeE6()/1E6 ) + "," + String.valueOf( pMid.getLatitudeE6()/1E6 ) );
Log.i("vvcar","max point is : " + String.valueOf( pMax.getLongitudeE6()/1E6 ) + "," + String.valueOf( pMax.getLatitudeE6()/1E6 ) );
Log.i("vvcar","min point is : " + String.valueOf( pMin.getLongitudeE6()/1E6 ) + "," + String.valueOf( pMin.getLatitudeE6()/1E6 ) );
//!!!!!这里不知道为什么,如果同时用getZoomToBound()和animateTo()两个函数,一定要把animateTo()放到getZoomToBound()后面,不然会无效
mMapController.setCenter(pMid);
float zoomlevel = mMapView.getZoomToBound(pMax,pMin);
mMapController.setZoom(zoomlevel);
mMapView.refresh();
}
/*
* 输入若干个坐标点,返回这些点的中心店和两个对角点
*/
public void getCenterAndEdg(ArrayList<GeoPoint> list,GeoPoint pMid,GeoPoint pMax,GeoPoint pMin){
double dMidLng = 0;
double dMidLat = 0;
double dMaxLng = (list.get(0).getLongitudeE6())/1E6;
double dMaxLat = (list.get(0).getLatitudeE6())/1E6;
double dMinLng = (list.get(0).getLongitudeE6())/1E6;
double dMinLat = (list.get(0).getLatitudeE6())/1E6;
for(int i = 0 ; i<list.size() ; i++ ){
double dLng = (list.get(i).getLongitudeE6())/1E6;
double dLat = (list.get(i).getLatitudeE6())/1E6;
Log.i("vvcar","point " + String.valueOf(i) + " info : " + String.valueOf(dLng) + "," + String.valueOf(dLat) );
if( dLng > dMaxLng ){
dMaxLng = dLng;
}
if( dLng < dMinLng ){
dMinLng = dLng;
}
if( dLat > dMaxLat ){
dMaxLat = dLat;
}
if( dLat < dMinLat ){
dMinLat = dLat;
}
}
//
dMidLng = (dMaxLng+dMinLng)/2;
dMidLat = (dMaxLat+dMinLat)/2;
pMid.setLongitudeE6( (int)(dMidLng * 1E6) );
pMid.setLatitudeE6( (int)(dMidLat * 1E6) );
//
pMax.setLongitudeE6( (int)(dMaxLng * 1E6) );
pMax.setLatitudeE6( (int)(dMaxLat * 1E6) );
pMin.setLongitudeE6( (int)(dMinLng * 1E6) );
pMin.setLatitudeE6( (int)(dMinLat * 1E6) );
}
class OverlayTest extends ItemizedOverlay<OverlayItem> {
//用MapView构造ItemizedOverlay
public OverlayTest(Drawable mark,MapView mapView){
super(mark,mapView);
}
protected boolean onTap(int index) {
//在此处理item点击事件
Log.i("map", "index is " + String.valueOf(index) );
return true;
}
public boolean onTap(GeoPoint pt, MapView mapView){
//在此处理MapView的点击事件,当返回 true时
super.onTap(pt,mapView);
Log.i("map","point is " + String.valueOf(pt.getLongitudeE6()) + " , " + String.valueOf(pt.getLatitudeE6()) );
//这里不能返回true,返回true就不调用了?
return false;
}
}
}
转载自:https://blog.csdn.net/ecjtu_lyc/article/details/84602754