Android与位置相关的应用开发(2)-MapController与Overlay

在前一节中演示了MapView的简单应用,MapView从服务的提供方获取相关数据并且将地图显示出来,此外通过MapView还可以显示如卫星图,交通等信息;在本文中将介绍另外两个重要类MapController和Overlay,这两个类将丰富地图的功能和展示。
1)MapController:
如果要在地图中设置缩放等级、实现缩放功能和动态移动等功能需要使用另一个重要的类MapController,MapController的简单使用如下:

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mapview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Drawable marker = getResources().getDrawable(R.drawable.mapmarker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
InterestingLocations interestPlaces = new InterestingLocations(marker);
mapView.getOverlays().add(interestPlaces);
GeoPoint pt = interestPlaces.getCenter();
mapView.getController().setCenter(pt);
mapView.getController().setZoom(15);
}

通过setCenter(GeoPoint)设置当前地图显示的中心位置,setZoom(zoomLevel)设置当前地图的缩放等级,通过zoomIn()和zoomOut()实现地图的缩放;
2)Overlay:
在使用地图的时候我们希望将我们感兴趣的位置在地图上标示出来,这个时候我们将使用覆盖图,将我们感兴趣的位置用覆盖图在地图上标示出来,android提供了一些简单的Overlay的实现方式,在这里演示最简单ItemizedOverlay,代码如下:

@SuppressWarnings("rawtypes")
class InterestingLocations extends ItemizedOverlay {
private Drawable marker;

private List<OverlayItem> locations = new ArrayList<OverlayItem>();

public InterestingLocations(Drawable defaultMarker) {
super(defaultMarker);
marker = defaultMarker;
GeoPoint disneyMagicKingdom = new GeoPoint((int) (28.418971 * 1E6),
(int) (-81.581436 * 1E6));
GeoPoint disneySevenLagoon = new GeoPoint((int) (28.410067 * 1E6),
(int) (-81.583699 * 1E6));
locations.add(new OverlayItem(disneyMagicKingdom, "Magic Kingdom",
"Magic Kingdom"));
locations.add(new OverlayItem(disneySevenLagoon, "Seven Sea Lagoon",
"Seven Sea Lagoon"));
populate();
}

@Override
protected OverlayItem createItem(int index) {
return locations.get(index);
}

@Override
public int size() {
return locations.size();
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
}
}

该类要求提供一个覆盖图id,然后将感兴趣的位置GeoPoint添加至位置列表中;
再通过MapView.getOverlays().Add(Overlay)将覆盖图列表添加至MapView上用于显示;
本节完整代码如下:
layout文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="your api key"
android:clickable="true" />

</LinearLayout>

Activity代码:

package com.local;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MappingOverlayActivity extends MapActivity {

private MapView mapView;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mapview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Drawable marker = getResources().getDrawable(R.drawable.mapmarker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
InterestingLocations interestPlaces = new InterestingLocations(marker);
mapView.getOverlays().add(interestPlaces);
GeoPoint pt = interestPlaces.getCenter();
mapView.getController().setCenter(pt);
mapView.getController().setZoom(14);
}

@Override
protected boolean isRouteDisplayed() {
return false;
}

@SuppressWarnings("rawtypes")
class InterestingLocations extends ItemizedOverlay {
private Drawable marker;

private List<OverlayItem> locations = new ArrayList<OverlayItem>();

public InterestingLocations(Drawable defaultMarker) {
super(defaultMarker);
marker = defaultMarker;
GeoPoint hubin = new GeoPoint((int) (30.254618 * 1E6),
(int) (120.163650 * 1E6));
GeoPoint mateng = new GeoPoint((int) (30.278637 * 1E6),
(int) (120.150260 * 1E6));
locations.add(new OverlayItem(hubin, "外婆家湖滨店", "外婆家湖滨店"));
locations.add(new OverlayItem(mateng, "外婆家马塍店", "外婆家马塍店"));
populate();
}

@Override
protected OverlayItem createItem(int index) {
return locations.get(index);
}

@Override
public int size() {
return locations.size();
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Paint paint = new Paint();
paint.setTextSize(12);
paint.setColor(Color.BLUE);
for (OverlayItem item : locations) {
Point point = mapView.getProjection().toPixels(item.getPoint(),
null);
canvas.drawText(item.getTitle(), point.x, point.y-20, paint);
}
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
}
}

运行结果如下:
[img]http://dl.iteye.com/upload/attachment/0070/7066/c0900a1e-622f-3cc6-8dec-5dafe422ce17.jpg[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值