技术开发 频道

用Google Map API开发Android应用五步

  【IT168技术】了解了Android位置服务和Google地图API后,下面将简要介绍在Android中实现定位处理的基本流程。

  (1)准备Activity类

  目标是使用Map API来显示地图,然后使用定位API来获取设备的当前定位信息以在Map上设置设备的当前位置,用户定位会随着用户的位置移动而发生改变。

  首先需要一个继承了MapActivity的Activity类,如下面的代码。

class MyGPSActivity extends MapActivity {
        …
        }

  要成功引用Map API,还必须先在AndroidManifest.xml中定义如下信息。

  (2)使用MapView

  要让地图显示的话,需要将MapView加入到应用中来。例如,在布局文件(main.xml)中加入如下代码。

<com.google.android.maps.MapView
           android:id
=”@+id/myGMap”
           android:layout_width
=”fill_parent”
           android:layout_height
=”fill_parent”
           android:enabled
=true
           android:clickable
=true
           android:apiKey
=”API_Key_String”
/>

  另外,要使用MAP服务的话,还需要一个API key。可以通过如下方式获取API key。

  1)找到USER_HOME\Local Settings\Application Data\Android目录下的debug.keystore文件。

  2)使用keytool工具来生成认证信息(MD5),使用如下命令行。

  keytool -list -alias androiddebugkey -keystore .keystore -storepass

  android -keypass android

  3)打开“Sign Up for the Android Maps API”页面,输入之前生成的认证信息(MD5)后将获取到API key。

  4)替换上面AndroidManifest.xml配置文件中“API_Key_String”为刚才获取的API key。

  注意:上面获取API key的介绍比较简单,在本章后面的内容中,将通过一个实例的实现过程来演示获取API key的方法。

  接下来继续补全MyGPSActivity类的代码,在此以使用MapView,例如下面的代码。

class MyGPSActivity extends MapActivity {
    @Override
    
public void onCreate(Bundle savedInstanceState) {
    
//创建并初始化地图
        gMapView
= (MapView) findViewById(R.id.myGMap);
            GeoPoint p
= new GeoPoint((int) (lat * 1000000), (int) (long * 1000000));
            gMapView.setSatellite(
true);
            mc
= gMapView.getController();
            mc.setCenter(p);
            mc.setZoom(
14);
    }
    …
}

  另外,如果要使用定位信息的话,必须设置一些权限,在AndroidManifest.xml中的具体配置如下。

<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”></uses-permission>
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”></uses-permission>

  (3)使用定位管理器

  可以通过使用Context.getSystemService方法,并传入Context.LOCATION_SERVICE参数获取定位管理器的实例。例如下面的代码。

  LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  之后,需要将原先的MyGPSActivity作一些修改,让它实现一个LocationListener接口,使其能够监听定位信息的改变。

class MyGPSActivity extends MapActivity implements LocationListener {

ublic void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
protected
boolean isRouteDisplayed() {
return
false;
}
}

  下面来添加一些代码,对LocationManager进行一些初始化工作,并在它的onCreate()方法中注册定位监听器。例如下面的代码。

@Override
public void onCreate(Bundle savedInstanceState) {
LocationManager lm
= (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
500.0f, this);
          }

  此时代码中的onLocationChanged方法就会在用户的位置发生500m距离的改变之后进行调用。这里默认使用的LocationProvider是“gps”(GSP_PROVIDER),但是可以根据需要,使用特定的Criteria对象调用LocationManger类的getBestProvider方法获取其他的 LocationProvider。以下代码是onLocationChanged方法的参考实现。

  public void onLocationChanged(Location location) {
    
if (location != null) {
    
double lat = location.getLatitude();
    
double lng = location.getLongitude();
     p
= new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
     mc.animateTo(p);
   }
}

  通过上面的代码,获取了当前的新位置并更新地图上的位置显示。还可以为应用程序添加一些诸如缩放效果,地图标注,文本等功能。

  (4)添加缩放控件

// 将缩放控件添加到地图上
ZoomControls zoomControls
=  (ZoomControls) gMapView.getZoomControls();
zoomControls.setLayoutParams(
new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
gMapView.addView(zoomControls);
gMapView.displayZoomControls(
true);

  (5)添加Map Overlay

  来到最后一步,添加Map Overlay。通过下面的代码可以定义一个overlay。

class MyLocationOverlay extends com.google.android.maps.Overlay {
  @Override
  
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
  super.draw(canvas, mapView, shadow);
  Paint paint
= new Paint();
  
// 将经纬度转换成实际屏幕坐标
  Point myScreenCoords
= new Point();
  mapView.getProjection().toPixels(p, myScreenCoords);
  paint.setStrokeWidth(
1);
  paint.setARGB(
255, 255, 255, 255);
  paint.setStyle(Paint.Style.STROKE);
  Bitmap bmp
= BitmapFactory.decodeResource(getResources(), R.drawable.marker);
  canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
  canvas.drawText(”how are you…”, myScreenCoords.x, myScreenCoords.y, paint);
  return
true;
}
}

  上面的overlay会在地图上显示一个“Here I am”的文本,然后把这个overlay添加到地图上去。

MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List
<Overlay> list = gMapView.getOverlays();
list.add(myLocationOverlay);
0
相关文章