public class maprouteactivity extends mapactivity{
线性布局线性布局;
地图视图;
私人道路道路;
@重写
public void oncreate(bundle savedinstanceState){
super.oncreate(保存的状态);
setContentView(r.layout.main);
mapview=(mapview)findView byid(r.id.mapview);
mapview.setbuiltinzoomcontrols(真);
新线程()
@重写
public void run()。{
双fromlat=49.85,fromlon=24.016667;
双Tolat=50.45,Tolon=30.523333;
string url=道路提供商
.geturl(从lat、fromlon、tolat、tolon);
inputstream=getConnection(url);
mroad=roadprovider.getRoute(is);
mhandler.sendEmptyMessage(0);
}
}.start();
}
handler mhandler=new handler()。{
公共void handlemessage(android.os.message msg){
textView textView=(textView)findView byid(r.id.description);
textview.settext(mroad.mname+“”+mroad.mdescription);
mapoverlay mapoverlay=新建mapoverlay(mroad,mapview);
list<overlay>listoOverlay=mapView.getOverlays();
listoOverlay.clear();
添加(mapoverlay);
mapview.invalidate();
};
};
专用输入流getConnection(字符串url){
inputstream=空;
尝试{
urlConnection conn=new url(url).openConnection();
是=conn.getinputstream();
}catch(异常异常){
e.printstacktrace();
}捕获(IOException E){
e.printstacktrace();
}
回报是;
}
@重写
受保护的Boolean IsRouteDisplayed()。{
返回错误;
}
}
< /代码>
请参见j2memaprouteandroidexon google code
为您提供路线信息的服务KML格式。
要获取kml文件,我们需要使用起始位置和目标位置组成url:
public static String getUrl(double fromLat, double fromLon,
double toLat, double toLon) {// connect to map web service
StringBuffer urlString = new StringBuffer();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");// from
urlString.append(Double.toString(fromLat));
urlString.append(",");
urlString.append(Double.toString(fromLon));
urlString.append("&daddr=");// to
urlString.append(Double.toString(toLat));
urlString.append(",");
urlString.append(Double.toString(toLon));
urlString.append("&ie=UTF8&0&om=0&output=kml");
return urlString.toString();
}
接下来,您将需要解析XML(使用SAXParser实现)并填充数据结构:
public class Point {
String mName;
String mDescription;
String mIconUrl;
double mLatitude;
double mLongitude;
}
public class Road {
public String mName;
public String mDescription;
public int mColor;
public int mWidth;
public double[][] mRoute = new double[][] {};
public Point[] mPoints = new Point[] {};
}
网络连接在Android和BlackBerry上以不同的方式实现,因此您必须首先形成URL:
public static String getUrl(double fromLat, double fromLon,
double toLat, double toLon)
然后创建与此URL的连接并获取inputstream。
然后传递这个输入流并得到解析的数据结构:
public static Road getRoute(InputStream is)
完整源代码RoadProvider.java
黑莓
Dead link - BlackBerry Storm screenshot
class MapPathScreen extends MainScreen {
MapControl map;
Road mRoad = new Road();
public MapPathScreen() {
double fromLat = 49.85, fromLon = 24.016667;
double toLat = 50.45, toLon = 30.523333;
String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
map = new MapControl();
add(new LabelField(mRoad.mName));
add(new LabelField(mRoad.mDescription));
add(map);
}
protected void onUiEngineAttached(boolean attached) {
super.onUiEngineAttached(attached);
if (attached) {
map.drawPath(mRoad);
}
}
private InputStream getConnection(String url) {
HttpConnection urlConnection = null;
InputStream is = null;
try {
urlConnection = (HttpConnection) Connector.open(url);
urlConnection.setRequestMethod("GET");
is = urlConnection.openInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
}
查看上的完整代码J2MEMapRouteBlackBerryEx关于谷歌代码
安卓
public class MapRouteActivity extends MapActivity {
LinearLayout linearLayout;
MapView mapView;
private Road mRoad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
new Thread() {
@Override
public void run() {
double fromLat = 49.85, fromLon = 24.016667;
double toLat = 50.45, toLon = 30.523333;
String url = RoadProvider
.getUrl(fromLat, fromLon, toLat, toLon);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
mHandler.sendEmptyMessage(0);
}
}.start();
}
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
TextView textView = (TextView) findViewById(R.id.description);
textView.setText(mRoad.mName + " " + mRoad.mDescription);
MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
};
};
private InputStream getConnection(String url) {
InputStream is = null;
try {
URLConnection conn = new URL(url).openConnection();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
查看上的完整代码J2MEMapRouteAndroidEx谷歌代码