作者:程溯
简介
Demo按类型往地图里添加POI兴趣点,并添加兴趣点的名称(name)与详细信息(detail)。地图上不同类型的POI兴趣点采用不同符号来表示。
示例数据
Data: mypoi.udb、mypoi.udd、mypoi.smwu
关键类型/成员
GeoStyle类
GeoPoint类
Environment类
WorkspaceConnectionInfo类
Map.setWorkspace()方法
MapControl.addGeometryAddedListener()方法
DatasetVector.query()方法
Recordset.edit()方法
Recordset.setGeometry()类
Recordset.setFieldValue()方法
Recordset.update()方法
使用步骤
(1) 将iMobile for android根目录的libs目录下的armeabi目录、com.supermap.data.jar、
com.supermap.mapping.jar拷贝至MyPoi工程目录的libs目录。
(2) elcipse导入MyPoi工程,如果需要,修改工程属性。
(3)需要将数据文件拷贝至android手机或模拟器的指定目录下。
(4) 编译运行程序,通过类型下拉列表选择POI类型,单击“添加”按钮,设置添加操作状态。
(5) 在地图上需要的位置单击,添加兴趣点,添加之后再单击“提交”按钮,提交操作。 在弹出的页面中填写名称和详细信息,单击“添加”按钮返回地图页面,地图窗口中显示了POI的符号和名称。
代码块
MainActivity:
import com.supermap.data.*;
import com.supermap.mapping.*;
import com.supermap.test.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.ZoomControls;
public class MainActivity extends Activity implements OnTouchListener,
GeometryAddedListener {
private final int REQUEST_INFO = 1001;
private MapControl mapControl;
private Workspace workspace;
private MapView mapView;
private ZoomControls zoomControls;
private Spinner spinnerPoiType;
private Button addButton;
private Button add;
private DatasetVector datasetVector;
private String strSymbolName = "银行";
private int nSymbolID = 907971;
private GeometryEvent curEvent;
private static final String[] types = { "银行", "医院", "小区", "停车场", "商场",
"公园", "药店", "小区", "餐厅" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Environment.setLicensePath(getString(R.string.license_path));
Environment.setTemporaryPath(getString(R.string.temp_path));
Environment.setWebCacheDirectory(getString(R.string.cache_path));
Environment.initialization(this);
setContentView(R.layout.main);
openData();
initView();
}
private void openData() {
workspace = new Workspace();
WorkspaceConnectionInfo info = new WorkspaceConnectionInfo();
info.setServer(getString(R.string.data_path));
info.setType(WorkspaceType.SMWU);
if (workspace.open(info)) {
mapView = (MapView) findViewById(R.id.mapview);
mapControl = mapView.getMapControl();
mapControl.getMap().setWorkspace(workspace);
boolean isOpenMap = mapControl.getMap().open("map");
if (isOpenMap) {
mapControl.addGeometryAddedListener(this);
mapControl.getMap().refresh();
Layer layer = mapControl.getMap().getLayers().get(1);
layer.setEditable(true);
Dataset dataset = workspace.getDatasources().get(0)
.getDatasets().get(0);
if (dataset != null) {
datasetVector = (DatasetVector) dataset;
}
}
}
}
private void initView() {
add = (Button) findViewById(R.id.add);
zoomControls = (ZoomControls) findViewById(R.id.zoomControls1);
zoomControls.setOnZoomOutClickListener(new OnClickListener() {
public void onClick(View v) {
mapControl.getMap().zoom(0.5);
mapControl.getMap().refresh();
}
});
zoomControls.setOnZoomInClickListener(new OnClickListener() {
public void onClick(View v) {
mapControl.getMap().zoom(2);
mapControl.getMap().refresh();
}
});
addButton = (Button) findViewById(R.id.btn_add);
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mapControl.setAction(Action.CREATEPOINT);
}
});
spinnerPoiType = (Spinner) findViewById(R.id.spn_poi_type);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, types);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerPoiType.setAdapter(adapter);
spinnerPoiType.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (spinnerPoiType.getSelectedItem() != null) {
strSymbolName = spinnerPoiType.getSelectedItem().toString();
}
strSymbolName = spinnerPoiType.getSelectedItem().toString();
if (strSymbolName.equals("")) {
return;
} else {
Symbol symbol = workspace.getResources().getMarkerLibrary()
.findSymbol(strSymbolName);
if (symbol != null) {
nSymbolID = symbol.getID();
} else {
nSymbolID = 0;
}
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void add(View view) {
mapControl.submit();
}
@Override
public void geometryAdded(GeometryEvent arg0) {
curEvent = arg0;
Intent intent = new Intent(getBaseContext(), InfoWnd.class);
intent.putExtra(InfoWnd.Poi_Type, strSymbolName);
startActivityForResult(intent, REQUEST_INFO);
}
public synchronized void onActivityResult(final int requestCode,
int resultCode, final Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_INFO) {
String strName = data.getStringExtra(InfoWnd.INFO_NAME);
String strDetail = data.getStringExtra(InfoWnd.INFO_DETAIL);
setPointInfo(strName, strDetail);
}
} else {
deletePoint();
}
mapControl.getMap().refresh();
mapControl.setAction(Action.PAN);
}
private void setPointInfo(String strName, String strDetail) {
try {
if (datasetVector == null) {
return;
}
String strFilter = "SmID = " + String.valueOf(curEvent.getID());
Recordset recordset = null;
recordset = datasetVector.query(strFilter, CursorType.DYNAMIC);
Geometry geometry = recordset.getGeometry();
GeoPoint geoPoint = null;
if (geometry != null) {
geoPoint = (GeoPoint) geometry;
GeoStyle geoStyle = new GeoStyle();
geoStyle.setMarkerSize(new Size2D(8, 8));
geoStyle.setMarkerSymbolID(nSymbolID);
geoPoint.setStyle(geoStyle);
}
recordset.edit();
if (geoPoint != null) {
recordset.setGeometry(geoPoint);
}
if (!strName.isEmpty()) {
recordset.setFieldValue("name", strName);
}
if (!strDetail.isEmpty()) {
recordset.setFieldValue("detail", strDetail);
}
recordset.update();
recordset.dispose();
mapControl.getMap().refresh();
} catch (Exception e) {
Log.e("MyPoit", e.getMessage());
}
}
private void deletePoint() {
if (datasetVector == null) {
return;
}
String strFilter = "SmID = " + String.valueOf(curEvent.getID());
Recordset recordset = null;
recordset = datasetVector.query(strFilter, CursorType.DYNAMIC);
recordset.delete();
recordset.dispose();
}
public boolean onTouch(View v, MotionEvent event) {
mapControl.onMultiTouch(event);
return true;
}
public void onDestroy() {
workspace.close();
super.onDestroy();
}
}
InfoWnd:
import com.supermap.test.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class InfoWnd extends Activity implements OnClickListener{
public static final String Poi_Type = "PoiType";
public static final String INFO_NAME = "Name";
public static final String INFO_DETAIL = "Detail";
private EditText etName;
private EditText etDetail;
private Button btnAdd;
private Button btnCancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.infownd);
etName = (EditText) findViewById(R.id.editName);
etDetail = (EditText) findViewById(R.id.editDetail);
btnAdd = (Button) findViewById(R.id.btn_add);
btnAdd.setOnClickListener(this);
btnCancel = (Button) findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(this);
etName = (EditText)findViewById(R.id.editName);
etDetail = (EditText)findViewById(R.id.editDetail);
String strName = getIntent().getStringExtra(Poi_Type);
etName.setText(strName);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public void onClick(View view) {
if (view == btnAdd) {
addPoint();
} else if (view == btnCancel) {
finish();
}
}
private void addPoint() {
String strName = etName.getText().toString();
String strDetail = etDetail.getText().toString();
getIntent().putExtra(INFO_NAME, strName);
getIntent().putExtra(INFO_DETAIL, strDetail);
setResult(RESULT_OK, getIntent());
finish();
}
}
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/map_panel"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<com.supermap.mapping.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</com.supermap.mapping.MapView>
<ZoomControls
android:id="@+id/zoomControls1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="@drawable/toolbar_selector"
android:paddingBottom="2dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="6dp" >
<TextView
android:id="@+id/txt_poi_type"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="@string/poitype"
android:textColor="#ff000000" />
<Spinner
android:id="@+id/spn_poi_type"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_toLeftOf="@+id/btn_add"
android:layout_toRightOf="@+id/txt_poi_type" />
<Button
android:id="@+id/btn_add"
android:layout_width="52dp"
android:layout_height="40dp"
android:layout_toLeftOf="@+id/add"
android:text="@string/stradd" />
<Button
android:id="@+id/add"
android:layout_width="52dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:onClick="add"
android:text="提交" />
</RelativeLayout>
</RelativeLayout>
infownd.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/inputname" />
<EditText
android:id="@+id/editDetail"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:hint="@string/inputdetail"
android:scrollbars="vertical" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stradd" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/strcancel" />
</LinearLayout>
</LinearLayout>
运行效果