在实际的移动开发过程中,地图相关的操作对于地理编码与地理反编码的使用都是十分普遍。幸运的是,
Android的
MapView控件中对于这两者都进行了封装,因此可以方便的利用
Google Map Service进行二者查询。下面将对开发过程做一个简单介绍。
Java代码:
复制代码
接着可以在主 Layout文件中加入对于地图的显示,这里需要加入刚才申请的 MapKey,否则地图将无法正常显示。
Java代码:
复制代码
接着在主 Activity的 JAVA文件进行修改,支持地图显示。
Java代码:
复制代码
此时运行程序,地图应该就可以正常显示了
效果图:
此时我们再向程序中加入地理编码与地理反编码的功能
Java代码:
复制代码
地理反编码,其中 MapOverlay为地图图层上的叠加图层,用于标识的显示以及点击事件的捕捉。
Java代码:
复制代码
效果图:
Java代码:
- < ?xml version="1.0" encoding="utf-8"?>
- < manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="net.learn2develop.GoogleMaps"
- android:versionCode="1"
- android:versionName="1.0.0">
- < application
- android:icon="@drawable/icon"
- android:label="@string/app_name">
- < uses-library
- android:name="com.google.android.maps" />
- < activity
- android:name=".MapsActivity"
- android:label="@string/app_name">
- < intent-filter>
- < action
- android:name="android.intent.action.MAIN" />
- < category
- android:name="android.intent.category.LAUNCHER" />
- < /intent-filter>
- < /activity>
- < /application>
- < uses-permission android:name="android.permission.INTERNET" />
- < /manifest>
接着可以在主 Layout文件中加入对于地图的显示,这里需要加入刚才申请的 MapKey,否则地图将无法正常显示。
Java代码:
- < ?xml version="1.0" encoding="utf-8"?>
- < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- < com.google.android.maps.MapView
- android:id="@+id/mapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:enabled="true"
- android:clickable="true"
- android:apiKey="MapKey"
- />
- < /RelativeLayout>
接着在主 Activity的 JAVA文件进行修改,支持地图显示。
Java代码:
- import com.google.android.maps.MapActivity;
- import com.google.android.maps.MapView;
- import android.os.Bundle;
- public class MapsActivity extends MapActivity
- {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- MapView mapView = (MapView) findViewById(R.id.mapView);
- mapView.setBuiltInZoomControls(true);
- }
- }
此时运行程序,地图应该就可以正常显示了
效果图:

此时我们再向程序中加入地理编码与地理反编码的功能
Java代码:
- Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
- try {
- List
- addresses = geoCoder.getFromLocationName("上海市杨浦区四平路1239号", 5);
- String add = "";
- if (addresses.size() > 0) {
- p = new GeoPoint(
- (int) (addresses.get(0).getLatitude() * 1E6),
- (int) (addresses.get(0).getLongitude() * 1E6));
- mc.animateTo(p);
- mapView.invalidate();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
地理反编码,其中 MapOverlay为地图图层上的叠加图层,用于标识的显示以及点击事件的捕捉。
Java代码:
- view plaincopy to clipboardprint?
- class MapOverlay extends com.google.android.maps.Overlay
- {
- @Override
- public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when)
- {
- //...
- }
- @Override
- public boolean onTouchEvent(MotionEvent event, MapView mapView)
- {
- //---when user lifts his finger---
- if (event.getAction() == 1) {
- GeoPoint p = mapView.getProjection().fromPixels(
- (int) event.getX(),
- (int) event.getY());
- Geocoder geoCoder = new Geocoder(
- getBaseContext(), Locale.getDefault());
- try {
- List
- addresses = geoCoder.getFromLocation(
- p.getLatitudeE6() / 1E6,
- p.getLongitudeE6() / 1E6, 1);
- String add = "";
- if (addresses.size() > 0)
- {
- for (int i=0; ii++)
- add += addresses.get(0).getAddressLine(i) + ";
- }
- Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- return true;
- }
- else
- return false;
- }
- }
效果图:

