Android引路蜂地图开发示例:路径查询
可以通过RasterMap的getDirection()方法来查询路径,和查询地址类似,路径查询的结果也是通过回调函数的方式来通知应用程序的,下面的例子返回南京到北京的路径。返回结果存放在MapDirection中,MapDirection包含了路径的详细信息,包括路径的每个步骤,长度,时间,方向等。
[java]
package com.pstreets.gisengine.demo;
import com.mapdigit.gis.MapDirection;
import com.mapdigit.gis.MapPoint;
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.service.IRoutingListener;
import com.pstreets.gisengine.R;
import com.pstreets.gisengine.SharedMapInstance;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MapRouting extends Activity implements IRoutingListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onStart() {
super.onStart();
GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
SharedMapInstance.map.setCenter(center, 15,
com.mapdigit.gis.raster.MapType.MICROSOFTCHINA);
SharedMapInstance.map.setRoutingListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.maprouting_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.getdirection:
String name1 = "南京";
String name2 = "北京";
SharedMapInstance.map.getDirections("from: " + name1 + " to: "
+ name2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void done(String query, MapDirection result) {
if (result != null) {
SharedMapInstance.map.setMapDirection(result);
SharedMapInstance.map.resize(result.getBound());
SharedMapInstance.map.zoomOut();
SharedMapInstance.map.zoomOut();
}
}
@Override
public void readProgress(int bytes, int total) {
}
}
package com.pstreets.gisengine.demo;
import com.mapdigit.gis.MapDirection;
import com.mapdigit.gis.MapPoint;
import com.mapdigit.gis.geometry.GeoLatLng;
import com.mapdigit.gis.service.IRoutingListener;
import com.pstreets.gisengine.R;
import com.pstreets.gisengine.SharedMapInstance;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MapRouting extends Activity implements IRoutingListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onStart() {
super.onStart();
GeoLatLng center = new GeoLatLng(32.0616667, 118.7777778);
SharedMapInstance.map.setCenter(center, 15,
com.mapdigit.gis.raster.MapType.MICROSOFTCHINA);
SharedMapInstance.map.setRoutingListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.maprouting_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.getdirection:
String name1 = "南京";
String name2 = "北京";
SharedMapInstance.map.getDirections("from: " + name1 + " to: "
+ name2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void done(String query, MapDirection result) {
if (result != null) {
SharedMapInstance.map.setMapDirection(result);
SharedMapInstance.map.resize(result.getBound());
SharedMapInstance.map.zoomOut();
SharedMapInstance.map.zoomOut();
}
}
@Override
public void readProgress(int bytes, int total) {
}
}
地图服务可以选择使用Google 地图服务,CloudMade地图服务,在中国还可能选择MapAbc地图服务,缺省使用Google 地图服务。
getDirections()具有三个重载函数,例子中是采用的文字描述方式。上述示例采用了from: address1 to: address2 的格式, CloudMade地图服务和MapAbc地图服务则必需采用 经度1,纬度1,经度2,纬度2和格式。
为避免混淆,可以使用下述格式。
public void getDirection(GeoLatLng[] waypoints, IRoutingListener listener);
其中 waypoints 为途径点坐标数组经纬值,可以支持多点路径查询。
此外对于MapAbc 地图服务,还可以指定城市编码,如南京编码为25。
public void getDirection(int citycode,String query, IRoutingListener listener);
作者:mapdigit
补充:移动开发 , Android ,