1 定义callout的外观,可以在一个xml中定义
<?xml version="1.0" encoding="utf-8"?> <resources> <calloutViewStyle titleTextColor="#000000" <!—标题颜色/> titleTextSize = 10; <!—标题字体大小/> titleTextStyle = 0; <!—标题字体/> backgroundColor="#ffffff" <!—背景颜色/> backgroundAlpha="255" <!—透明度 0(透明) to 255(不透明) /> frameColor="#000000" <!—边框颜色/> flat="true" <!—是否会在3D效果/> cornerCurve="0" <!—圆角的度数/> anchor="5" /> <!—锚的位置(0-8)/> </resources>
2 定义callout要显示的内容,可以在一个layout中定义。
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextViewandroid:text="这个是callout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3 显示callout,以下代码实现单击屏幕的时候,把callout显示出来
public class CalloutTestActivity extends Activityimplements OnSingleTapListener {
MapViewmMapView ;
private Calloutcallout;
/** Called when the activity is first created. */
@Override
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMapView =(MapView)findViewById(R.id.mapView);
ArcGISTiledMapServiceLayerarm=new ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer");
mMapView.addLayer(arm);
mMapView.setOnSingleTapListener(this);
callout=mMapView.getCallout();//得到MapView的Callout,一个MapView只能有一个Callout
}
public void onSingleTap(float arg0,float arg1) {
Pointp=mMapView.toMapPoint(new Point(arg0, arg1));
callout.setStyle(R.xml.colloutxml);//设置显示样式(第1步定义的xml)
callout.setContent(View.inflate(this, R.layout.colloutlay,null));//把第2步的定义的layout作为callout的显示内容
callout.setCoordinates(p); //设置显示的位置为单击的位置
callout.show();//显示callout
}
}
4 效果