Callout气泡的使用

首先我们要获取MapView中的气泡,通过MapView的getCallout()方法获取一个气泡。

Callout会实现的功能就是当你单击地图上一个标注的时候,会弹出一个衔套在MapView之上的弹窗,里面可以显示该标注的一些信息和属性。

Callout的API

效果图:
这里写图片描述

代码:

public class CalloutActivity extends AppCompatActivity {

    private MapView mMapView;

    private String mapServerUrl = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer";

    private Callout mCallout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_callout);

        mMapView = (MapView) findViewById(R.id.mapview);

        ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(mapServerUrl);
        mMapView.addLayer(arcGISTiledMapServiceLayer);

        mMapView.setOnSingleTapListener(new OnSingleTapListener() {
            @Override
            public void onSingleTap(float v, float v1) {
                Point point = mMapView.toMapPoint(v, v1);

                initCallout(point);
            }
        });
    }

    private void initCallout(Point point) {
        //获取一个气泡
        mCallout = mMapView.getCallout();

        //设置最大的长宽
        mCallout.setMaxWidth(500);
        mCallout.setMaxHeight(500);

        View callView = View.inflate(this, R.layout.callout, null);

        CalloutStyle calloutStyle = new CalloutStyle();
        //设置尖尖角的位置
        calloutStyle.setAnchor(Callout.ANCHOR_POSITION_LOWER_MIDDLE);
        mCallout.setStyle(calloutStyle);

        //也通过xml方式设置Callout的Style,新建一个xml放在res/xml下
        //mCallout.setStyle(R.xml.calloutstyle);

        mCallout.setOffset(0, -15);
        mCallout.show(point,callView);
    }


    protected void onResume() {
        super.onResume();
        mMapView.unpause();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.pause();
    }
也可以通过xml方式设置Callout的Style,新建一个xml放在res/xml下
 <?xml version="1.0" encoding="utf-8"?>
  <resources>
      <calloutViewStyle
          anchor="5"
          backgroundAlpha="255"
          backgroundColor="#ffffff"
          cornerCurveDp="20"
          frameColor="#000000"
          maxHeightDp="300"
          maxWidthDp="500" />
  </resources>

只需调用callout.setStyle方法来设置:

mCallout.setStyle(R.xml.calloutstyle);

或者在布局文件中设置:

 <com.esri.android.map.MapView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/mapview" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     calloutStyle="@xml/style3" />
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐