brief introduction
Demo adds POI interest points to the map by type, and adds name and detail of interest points. Different types of POI interest points on maps are represented by different symbols.
Sample data
Data: mypoi.udb,mypoi.udd,mypoi.smwu
Key types/members
GeoStyle class
GeoPoint class
Environment class
Workspace ConnectionInfo class
Map.setWorkspace() method
MapControl.addGeometryAddedListener() method
DatasetVector.query() method
Recordset.edit() method
Recordset.setGeometry() class
Recordset.setFieldValue() method
Recordset.update() method
Use steps
(1) armeabi directory, com.supermap.data.jar under libs directory of iMobile for android root directory,
Copy com.supermap.mapping.jar to the libs directory of the MyPoi project directory.
(2) elcipse imports the MyPoi project and modifies the project properties if necessary.
(3) The data files need to be copied to the specified directory of the android phone or simulator.
(4) Compile the running program, select the POI type through the type drop-down list, click the "Add" button, and set the add operation status.
(5) Click where you need it on the map, add points of interest, and then click the "Submit" button to submit the operation. Fill in the name and details in the pop-up page, click the "Add" button to return to the map page, and the symbol and name of POI are displayed in the map window.
code block
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 = "Bank";
private int nSymbolID = 907971;
private GeometryEvent curEvent;
private static final String[] types = { "Bank", "Hospital", "Residential quarters", "Parking lot", "Market",
"Park", "Pharmacy", "Residential quarters", "Restaurant" };
@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="Submitted" />
</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>