Android 地图的地理编码与地理反编码

来源:互联网 发布:飞凡网络 编辑:程序博客网 时间:2024/04/30 03:37
在实际的移动开发过程中,地图相关的操作对于地理编码与地理反编码的使用都是十分普遍。幸运的是,AndroidMapView控件中对于这两者都进行了封装,因此可以方便的利用Google Map Service进行二者查询。下面将对开发过程做一个简单介绍。

Java代码:

  1. < ?xml version="1.0" encoding="utf-8"?>

  2. < manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="net.learn2develop.GoogleMaps"

  4. android:versionCode="1"
  5. android:versionName="1.0.0">

  6. < application
  7. android:icon="@drawable/icon"
  8. android:label="@string/app_name">

  9. < uses-library
  10. android:name="com.google.android.maps" />

  11. < activity
  12. android:name=".MapsActivity"
  13. android:label="@string/app_name">

  14. < intent-filter>

  15. < action
  16. android:name="android.intent.action.MAIN" />

  17. < category
  18. android:name="android.intent.category.LAUNCHER" />

  19. < /intent-filter>

  20. < /activity>

  21. < /application>

  22. < uses-permission android:name="android.permission.INTERNET" />

  23. < /manifest>

复制代码

                接着可以在主Layout文件中加入对于地图的显示,这里需要加入刚才申请的MapKey,否则地图将无法正常显示。

Java代码:

  1. < ?xml version="1.0" encoding="utf-8"?>

  2. < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">

  5. < com.google.android.maps.MapView

  6. android:id="@+id/mapView"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:enabled="true"
  10. android:clickable="true"
  11. android:apiKey="MapKey"
  12. />

  13. < /RelativeLayout>

复制代码

                接着在主ActivityJAVA文件进行修改,支持地图显示。

Java代码:
  1. import com.google.android.maps.MapActivity;
  2. import com.google.android.maps.MapView;
  3. import android.os.Bundle;

  4. public class MapsActivity extends MapActivity

  5. {

  6. /** Called when the activity is first created. */

  7. @Override
  8. public void onCreate(Bundle savedInstanceState)
  9. {

  10. super.onCreate(savedInstanceState);

  11. setContentView(R.layout.main);

  12. MapView mapView = (MapView) findViewById(R.id.mapView);

  13. mapView.setBuiltInZoomControls(true);

  14. }

  15. }

复制代码

                 此时运行程序,地图应该就可以正常显示了

效果图:
xw4.png
2011-3-10 16:41 上传
下载附件(119.3 KB)


                此时我们再向程序中加入地理编码与地理反编码的功能

Java代码:


  1. Geocoder geoCoder = new Geocoder(this, Locale.getDefault());

  2. try {

  3. List

  4. addresses = geoCoder.getFromLocationName("上海市杨浦区四平路1239号", 5);

  5. String add = "";

  6. if (addresses.size() > 0) {

  7. p = new GeoPoint(


  8. (int) (addresses.get(0).getLatitude() * 1E6),

  9. (int) (addresses.get(0).getLongitude() * 1E6));

  10. mc.animateTo(p);

  11. mapView.invalidate();

  12. }

  13. } catch (IOException e) {

  14. e.printStackTrace();

  15. }

复制代码

                 地理反编码,其中MapOverlay为地图图层上的叠加图层,用于标识的显示以及点击事件的捕捉。

Java代码:
  1. view plaincopy to clipboardprint?

  2. class MapOverlay extends com.google.android.maps.Overlay

  3. {

  4. @Override
  5. public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when)

  6. {

  7. //...

  8. }

  9. @Override
  10. public boolean onTouchEvent(MotionEvent event, MapView mapView)

  11. {

  12. //---when user lifts his finger---

  13. if (event.getAction() == 1) {

  14. GeoPoint p = mapView.getProjection().fromPixels(

  15. (int) event.getX(),

  16. (int) event.getY());

  17. Geocoder geoCoder = new Geocoder(

  18. getBaseContext(), Locale.getDefault());

  19. try {

  20. List

  21. addresses = geoCoder.getFromLocation(

  22. p.getLatitudeE6() / 1E6,

  23. p.getLongitudeE6() / 1E6, 1);

  24. String add = "";

  25. if (addresses.size() > 0)

  26. {

  27. for (int i=0; ii++)
  28. add += addresses.get(0).getAddressLine(i) + ";

  29. }

  30. Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();

  31. }

  32. catch (IOException e) {

  33. e.printStackTrace();

  34. }

  35. return true;

  36. }

  37. else
  38. return false;

  39. }

  40. }

复制代码

效果图:
4.jpg
2011-3-10 16:46 上传
下载附件(10.99 KB)
            5.jpg
原创粉丝点击