论坛首页 移动开发技术论坛

【Android】说做就做:都市列表+卫星地图

浏览 26859 次
该帖已经被评为良好帖
作者 正文
   发表时间:2007-11-19  
前言:为了得奖。。。android。。。T_T。。。

完成notepad 3部曲,又看了其他人的MapView使用例子,手痒啊,做了个简陋的都市列表,并用MapView来看卫星地图,还有目前路况(traffic,如果有的话),Zoom In/Out,纯抄袭,羞愧中。。。

基本上基于notepad样板,做了个City的list:(偷工减料,static一下了,其实应该从db或者web service上面拿。。。)
java 代码
  1. static {  
  2.         cities = new ArrayList<String>();  
  3.         cities.add("beijing");  
  4.         cities.add("london");  
  5.         cities.add("bangalore");  
  6.         cities.add("potsdam");  
  7.         cities.add("brasilia");  
  8.           
  9.         geocodes = new HashMap<String, GeoPoint>();  
  10.         geocodes.put("beijing"new GeoPoint(40.25f,116.5f));  
  11.         geocodes.put("london"new GeoPoint(51.52f,-0.1f));  
  12.         geocodes.put("bangalore"new GeoPoint(12.97f,77.56f));  
  13.         geocodes.put("potsdam"new GeoPoint(52.4f,13.07f));  
  14.         geocodes.put("brasilia"new GeoPoint(-15.78f,-47.91f));  
  15.           
  16.     }  
geocode是从IBM一个Perl的例子上copy的。。。

接下来把notepad copy一下,改成自己的东西。。。
java 代码
  1. @Override  
  2.     protected void onCreate(Bundle icicle) {  
  3.         super.onCreate(icicle);  
  4.         setContentView(R.layout.city_list);  
  5.         fillData();  
  6.     }  
  7.       
  8.     private void fillData() {  
  9.         ArrayAdapter<String> cities =   
  10.             new ArrayAdapter<String>(this, R.layout.city_row, MapConstants.cities);  
  11.         setListAdapter(cities);  
  12.     }  
  13.       
  14.       
  15.     @Override  
  16.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  17.         super.onListItemClick(l, v, position, id);  
  18.   
  19.         Intent i = new Intent(this, CityMap.class); 
  20. //这里传递选择的城市名字
  21.         i.putExtra(MapConstants.CITY_NAME, MapConstants.cities.get(position));  
  22.         startSubActivity(i, ACTIVITY_VIEW);  
  23.     }  
  24.      
  25. //mapview 回来的时候显示城市list
  26.     @Override  
  27.     protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {  
  28.         super.onActivityResult(requestCode, resultCode, data, extras);  
  29.         fillData();  
  30.     }  

现在是重点了,CityMap:
java 代码
  1. public class CityMap extends MapActivity  

要extends MapActivity,因为是个View嘛。。。

在onCreate()里面拿到传递进去的city,然后拿city的geocode:
java 代码
  1. private MapView myMapView;  
  2.   
  3.     @Override  
  4.     public void onCreate(Bundle icicle) {  
  5.         super.onCreate(icicle);  
  6.           
  7.         Bundle extras = getIntent().getExtras();  
  8.           
  9.         // get the geopoint by city  
  10.         String city = extras.getString(MapConstants.CITY_NAME);  
  11.           
  12.         GeoPoint gp = MapConstants.geocodes.get(city);  

GeoPoint是个DTO/VO,里面就放两个float的经度和纬度:
java 代码
  1. public class GeoPoint {  
  2.   
  3.     private float latitude;  
  4.     private float longitude;  

java 代码
  1. myMapView = new MapView(this);  
  2.  
  3. Point p = new Point((int) (gp.getLatitude() * 1000000),  
  4.                               (int) (gp.getLongitude() * 1000000));  
  5.         // 地图控制器。。。
  6.         MapController mc = myMapView.getController();  
  7.        
  8.         mc.animateTo(p);  
  9.         // 21是最zoom in的一级,一共是1-21级
  10.         mc.zoomTo(21);  

  11.         setContentView(myMapView);  
  12.         // 切换到卫星地图
  13.         myMapView.toggleSatellite();  

搞定显示,接下来是key events:
i: zoom in
o: zoom out
s: satellite
t: traffic
java 代码
  1. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  2.         if (keyCode == KeyEvent.KEYCODE_I) {  
  3.             // zoom in
  4.             myMapView.getController().zoomTo(myMapView.getZoomLevel() + 1);  
  5.             return true;  
  6.         } else if (keyCode == KeyEvent.KEYCODE_O) {  
  7.             // zoom out 
  8.             myMapView.getController().zoomTo(myMapView.getZoomLevel() - 1);  
  9.             return true;  
  10.         } else if (keyCode == KeyEvent.KEYCODE_S) {  
  11.           // 卫星地图
  12.             myMapView.toggleSatellite();  
  13.             return true;  
  14.         } else if (keyCode == KeyEvent.KEYCODE_T) {  
  15.             // traffic,路况
  16.             myMapView.toggleTraffic();  
  17.             return true;  
  18.         }  
  19.         return false;  
  20.     }   

最后加个回到城市list的menu:

java 代码
  1. @Override  
  2.     public boolean onCreateOptionsMenu(Menu menu) {  
  3.         super.onCreateOptionsMenu(menu);  
  4.         menu.add(0, EXIT_ID, R.string.exit_citymap);  
  5.         return true;  
  6.     }  
  7.       
  8.     @Override  
  9.     public boolean onMenuItemSelected(int featureId, Item item) {  
  10.         super.onMenuItemSelected(featureId, item);  
  11.         switch(item.getId()) {  
  12.         case EXIT_ID:  
  13.             finish();  
  14.             break;  
  15.         }  
  16.           
  17.         return true;  
  18.     }  


   发表时间:2007-11-19  
靠!有附件就发不上去,去掉附件就可以了,JE大bug啊。。。附件续上。。。
  • 大小: 17.9 KB
  • 大小: 37 KB
0 请登录后投票
   发表时间:2007-11-19  
我用adb install lordhong.MapDemo1.apk,然后显示0 bytes虾米的,emulator里面也找不到。。。怎么回事?严格按照google doc:
Running an Android Application

To run a compiled application, you will upload the .apk file to the /data/app/ directory in the emulator using the adb tool as described here:

   1. Start the emulator (run <your_sdk_dir>/tools/emulator from the command line)
   2. On the emulator, navigate to the home screen (it is best not to have that application running when you reinstall it on the emulator; press the Home key to navigate away from that application).
   3. Run adb install myproject/bin/<appname>.apk to upload the executable. So, for example, to install the Lunar Lander sample, navigate in the command line to <your_sdk_dir>/sample/LunarLander and type ../../tools/adb install bin/LunarLander.apk
   4. In the emulator, open the list of available applications, and scroll down to select and start your application.

—T.T...
0 请登录后投票
   发表时间:2007-11-19  
android做出来的东西可以直接在手机上看?
0 请登录后投票
   发表时间:2007-11-19  
今天javaeye是怎么了? 老打不开自己的博客?
0 请登录后投票
   发表时间:2007-11-19  
lordhong 写道
我用adb install lordhong.MapDemo1.apk,然后显示0 bytes虾米的,emulator里面也找不到。。。怎么回事?严格按照google doc:
Running an Android Application

To run a compiled application, you will upload the .apk file to the /data/app/ directory in the emulator using the adb tool as described here:

   1. Start the emulator (run <your_sdk_dir>/tools/emulator from the command line)
   2. On the emulator, navigate to the home screen (it is best not to have that application running when you reinstall it on the emulator; press the Home key to navigate away from that application).
   3. Run adb install myproject/bin/<appname>.apk to upload the executable. So, for example, to install the Lunar Lander sample, navigate in the command line to <your_sdk_dir>/sample/LunarLander and type ../../tools/adb install bin/LunarLander.apk
   4. In the emulator, open the list of available applications, and scroll down to select and start your application.

—T.T...


进 adb shell里 看不到?? 如果能看到 rm了 然后从新 push吧
0 请登录后投票
   发表时间:2007-11-19  
lordhong 写道
我用adb install lordhong.MapDemo1.apk,然后显示0 bytes虾米的,emulator里面也找不到。。。怎么回事?严格按照google doc:
Running an Android Application

To run a compiled application, you will upload the .apk file to the /data/app/ directory in the emulator using the adb tool as described here:

   1. Start the emulator (run <your_sdk_dir>/tools/emulator from the command line)
   2. On the emulator, navigate to the home screen (it is best not to have that application running when you reinstall it on the emulator; press the Home key to navigate away from that application).
   3. Run adb install myproject/bin/<appname>.apk to upload the executable. So, for example, to install the Lunar Lander sample, navigate in the command line to <your_sdk_dir>/sample/LunarLander and type ../../tools/adb install bin/LunarLander.apk
   4. In the emulator, open the list of available applications, and scroll down to select and start your application.

—T.T...


进 adb shell里 看不到?? 如果能看到 rm了 然后从新 push吧
0 请登录后投票
   发表时间:2007-11-19  
试了下
adb shell 里已经有了.
重新rm掉.再install,或push也不行
0 请登录后投票
   发表时间:2007-11-20  
今天就adb shell。。。rm了一把,NND,程序都改了,emulator竟然不把新的app load进去,真是白痴。。。害我debug半个多小时。。,
0 请登录后投票
   发表时间:2007-11-20  
好东西, 不知道以后做这种手机软件的公司会不会多, 前景好不好啊!
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics