如何在 OverlayItem 中展示 图片和文字

dhz123 2010-03-02 03:41:16
package com.mainaer.android.Map;

import java.util.ArrayList;

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

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;

import com.google.android.maps.OverlayItem;

public class GeoItemizedOverlay extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

public GeoItemizedOverlay(Drawable defaultMarker) {
super( boundCenterBottom(defaultMarker));

}

@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}

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

public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}

public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
for (int index = size() - 1; index >= 0; index--) {
OverlayItem overLayItem = getItem(index);
/* 题目 */
String title = overLayItem.getTitle();
/* 简介 */
String snippet = overLayItem.getSnippet();
/* 象素点取得转换 */
Point point = projection.toPixels(overLayItem.getPoint(), null);

/* 目标城市圈出来 */
Paint paintCircle = new Paint();
paintCircle.setColor(Color.YELLOW);
canvas.drawCircle(point.x, point.y, 5, paintCircle);

/* 文字设置 */
Paint paintText = new Paint();
paintText.setColor(Color.RED);
paintText.setTextSize(25);

canvas.drawText(title+snippet, point.x, point.y, paintText);
}
super.draw(canvas, mapView, shadow);
}

}

在 onCreate 里面
调用:
public class WhereToBuy extends MapActivity {

MapView mMap;
TextView mCurrentCityName;
EditText mKey;
ImageButton mSwitchCity;
ImageButton mSearch;
Location mCurrentLocation;
GpsLocationListener mGpsLocation;
MapController mapController;

GeoItemizedOverlay itemizedOverlay;
Drawable drawable;
List<Overlay> mapOverlays;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initCtrl();

mMap.setBuiltInZoomControls(true);
mMap.setClickable(true);
mMap.setEnabled(true);

GeoPoint point1 = new GeoPoint(19240000, -99120000);
GeoPoint point2 = new GeoPoint(35410000, 139460000);
// 设置初始地图的中心位置
GeoPoint geoBeijing=new GeoPoint((int)(39.95*1000000), (int)(116.37*1000000));

OverlayItem bjItem = new OverlayItem(geoBeijing, "JQQ", "Bei jing Welcome you !!");

OverlayItem overlayitem1 = new OverlayItem(point1, "DHZ", "what are you doing now ?");
overlayitem1.setMarker(drawable);
OverlayItem overlayitem2 = new OverlayItem(point2, "SRH", "where are you from ?");
overlayitem1.setMarker(drawable);

itemizedOverlay.addOverlay(overlayitem1);
itemizedOverlay.addOverlay(overlayitem2);
itemizedOverlay.addOverlay(bjItem);

mapOverlays.add(itemizedOverlay);
mapController.animateTo(geoBeijing);
mapController.setCenter(geoBeijing);
mapController.setZoom(9);

}

protected void initCtrl() {
mMap = (MapView) findViewById(R.id.mMap);
mCurrentCityName = (TextView) findViewById(R.id.mCurrentCity);
mKey = (EditText) findViewById(R.id.mKey);
mSwitchCity = (ImageButton) findViewById(R.id.mSwitchCity);
mSearch = (ImageButton) findViewById(R.id.mSearch);
mapController = mMap.getController();
mapOverlays = mMap.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.androidlogo);
itemizedOverlay = new GeoItemizedOverlay(drawable);
}

protected void initLocation() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 从GPS获取位置信息,并且是每隔1000ms更新一次,并且不考虑位置的变化。
// 最后一个参数是LocationListener的一个引用,我们必须要实现这个类。
float fp = (float) 0.0;
mGpsLocation = new GpsLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, fp,mGpsLocation);
mCurrentLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mGpsLocation.setLocation(mCurrentLocation);

}

protected void initMapWithSpecialLocation(Location location) {

Log.i("GPS", "Init Overlay Arguments");



}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

怎么样才能显示文资 图片 和链接呢??
...全文
2962 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Smox 2011-01-20
  • 打赏
  • 举报
回复

private void addShopIcon(){
GeoPoint geoBeiJing = new GeoPoint((int) 39.95 * 1000000,
(int) 116.37 * 1000000);
Drawable shopIcon = this.getResources().getDrawable(R.drawable.shop);
MyShopOverlay myOverlay = new MyShopOverlay(shopIcon);
OverlayItem item1 = new OverlayItem(geoBeiJing, storeName, address);
// item1.setMarker(shopIcon);
myOverlay.addItem(item1);
map.getOverlays().add(myOverlay);
}
protected class MyShopOverlay extends ItemizedOverlay<OverlayItem>{

public MyShopOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}

private ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>();

@Override
protected OverlayItem createItem(int i) {
return overlayItems.get(i);
}

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

public void addItem(OverlayItem item){
overlayItems.add(item);
this.populate();
}

// @Override
// public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Projection proj = map.getProjection();
// for(OverlayItem item:overlayItems){
// //title
// String title = item.getTitle();
// //snippet
// String snippet = item.getSnippet();
// //geopoints to pixels
// Point point = proj.toPixels(item.getPoint(), null);
// }
// super.draw(canvas, mapView, shadow);
// }

}
皓月明 2010-12-03
  • 打赏
  • 举报
回复
楼上的效果很不错,是否给个例子呢?我的qq 379953191,邮箱mingmingsuper@gmail.com给发个例子呗
zd7451676 2010-05-17
  • 打赏
  • 举报
回复
我只能做这种效果图。。其实它的外框可以用图片代替 然后触发ImageButton.OnClickListener()去传值到下个Activity。
在draw()里面自定义2个方法 一个地图上表示图片具体位置,另外一个就是窗口里出现的东西 比如文字 图片。
急匆匆路过不知道这思想适合你不。。
79cy 2010-05-17
  • 打赏
  • 举报
回复
up!!!!!!!!
aide1986 2010-03-08
  • 打赏
  • 举报
回复
友情帮顶。。。。....

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧