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 效果