要实现的功能如下:首先点击menu会显示两条路线(仅仅做简单的模拟),点击路线之后去下载本条路线实时的车辆信息,并且画在地图上。点击另外一条路线的时候也执行以上操作,并且把原来的图像擦除。
好,我们首先新建一个工程TestActivity,继承与MapActivity,同时在layout的main.xml文件中加上mapview控件
<com.google.Android.maps.MapView android:clickable="true" android:layout_width="fill_parent" android:id="@+id/mapView" android:layout_height="fill_parent" android:apiKey="0GPDZeAXmGQ3vHfDbNitJWUp-lghBsV8jouMc5g" ></com.google.android.maps.MapView>
还有哦,不要忘记在androidmanifest中添加上<uses-library android:name="com.google.android.maps" />,至于权限什么的,多加几条就多加几条吧
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
好,到此为止,至少打开我们新建的工程能够正常的显示google地图了。
接下来,变量初始化一下
mapView = (MapView) findViewById(R.id.mapView); mapView.setStreetView(true); mapView.setBuiltInZoomControls(true); mapOverlays = mapView.getOverlays(); myloctionController=mapView.getController();
然后模拟个两条路线吧,在menu里面menu.add(0,0,0,"choose a route");在这个选项的点击响应中添加个函数showroutelist(),routestrings里面当然是放两条路线咯
private void showRouteList() { // TODO Auto-generated method stub final ArrayList<String> routeStrings=new ArrayList<String>(); routeStrings.add("1"); routeStrings.add("10"); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.item, R.id.textViewId, routeStrings); AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("当前两条路线"); builder.setAdapter(adapter, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub achieveAllDirection(routeStrings.get(which)); } }); builder.show(); }