通过调用从MapView的getCallout()获取Callout对象。
官方API:
首先来看下官方案例:
mMapView = (MapView) findViewById(R.id.mapView);
final ArcGISMap mMap = new ArcGISMap(Basemap.Type.TOPOGRAPHIC, 34.056295, -117.195800, 10);
mMapView.setMap(mMap);
mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
@Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
Log.d(sTag, "onSingleTapConfirmed: " + motionEvent.toString());
// get the point that was clicked and convert it to a point in map coordinates
android.graphics.Point screenPoint = new android.graphics.Point(Math.round(motionEvent.getX()),
Math.round(motionEvent.getY()));
// create a map point from screen point
Point mapPoint = mMapView.screenToLocation(screenPoint);
// convert to WGS84 for lat/lon format
Point wgs84Point = (Point) GeometryEngine.project(mapPoint, SpatialReferences.getWgs84());
// create a textview for the callout
TextView calloutContent = new TextView(getApplicationContext());
calloutContent.setTextColor(Color.BLACK);
calloutContent.setSingleLine();
// format coordinates to 4 decimal places
calloutContent.setText("Lat: " + String.format("%.4f", wgs84Point.getY()) + ", Lon: " + String.format("%.4f", wgs84Point.getX()));
// get callout, set content and show
mCallout = mMapView.getCallout();
mCallout.setLocation(mapPoint);
mCallout.setContent(calloutContent);
mCallout.show();
// center on tapped point
mMapView.setViewpointCenterAsync(mapPoint);
return true;
}
});
自己写个例子来信息了解Callout的相关属性
案例:在Callout中显示ListView
效果图:
public class CalloutActivity extends AppCompatActivity {
private MapView mMapView;
private List<CalloutBean> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_callout);
mMapView = (MapView) findViewById(R.id.mapview);
final ArcGISMap mMap = new ArcGISMap(Basemap.Type.TOPOGRAPHIC, 34.056295, -117.195800, 10);
mMapView.setMap(mMap);
initData();
mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Point mapPoint = mMapView.screenToLocation(new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY())));
showCallout(mapPoint);
return true;
}
});
}
private void showCallout(Point mapPoint) {
View calloutView = View.inflate(this, R.layout.callout_view, null);
ListView listView = calloutView.findViewById(R.id.listview);
CalloutAdapter calloutAdapter = new CalloutAdapter(this, dataList, R.layout.item_list_callout);
listView.setAdapter(calloutAdapter);
Callout callout = mMapView.getCallout();
//设置Callout样式
Callout.Style style = new Callout.Style(this);
style.setMaxWidth(400); //设置最大宽度
style.setMaxHeight(300); //设置最大高度
style.setMinWidth(200); //设置最小宽度
style.setMinHeight(100); //设置最小高度
style.setBorderWidth(2); //设置边框宽度
style.setBorderColor(Color.BLUE); //设置边框颜色
style.setBackgroundColor(Color.WHITE); //设置背景颜色
style.setCornerRadius(8); //设置圆角半径
//style.setLeaderLength(50); //设置指示性长度
//style.setLeaderWidth(5); //设置指示性宽度
style.setLeaderPosition(Callout.Style.LeaderPosition.LOWER_MIDDLE); //设置指示性位置
callout.setStyle(style);
callout.setContent(calloutView);
//通过在地图坐标中指定Point来设置Callout的位置。
callout.setLocation(mapPoint);
callout.show();
mMapView.setViewpointCenterAsync(mapPoint);
}
private void initData() {
dataList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
CalloutBean calloutBean = new CalloutBean("条目" + i, i + "");
dataList.add(calloutBean);
}
}
}
样式还可以写在xml文件中,通过以下方式加载:
Callout.Style style = new Callout.Style(this,R.xml.callout);
样式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<calloutStyle
backgroundColor="#FFC0C0C0" // opaque gray
borderColor="@android:color/white"
borderWidth="4"
cornerRadius="20"
maxHeight="500"
maxWidth="500"
minHeight="100"
minWidth="200"
leaderPosition="AUTOMATIC"
leaderLength="10"
leaderWidth="20"
/>
</resources>