Да, это намного проще в Android.
Вот код:
Объявите эти переменные в вашем классе MapActivity (или лучше об этом читайте в Интернете)
private List<Overlay> mapOverlays;
private CustomItemizedOverlay customItemizedOverlay;
в вашем onCreate ()
mapOverlays = mapView.getOverlays();
customItemizedOverlay = new CustomItemizedOverlay(
GoogleMapsActivity.this);
if (arrayList != null) {
//here Marker is my custom class which contains GeoPoints
for (Marker marker : arrayList) {
GeoPoint point = new GeoPoint(
(int) (marker.getLat() * 1E6),
(int) (marker.getLng() * 1E6));
OverlayItem overlayitem = new OverlayItem(point,
marker.getTitle(), marker.getDescription());
customItemizedOverlay.addOverlay(overlayitem);
}
//finally add all GeoPoints to the mapOverlays
mapOverlays.add(customItemizedOverlay);
if (arrayList.size() > 0) {
GeoPoint geoPoint = new GeoPoint((int) (arrayList.get(1)
.getLat() * 1E6),
(int) (arrayList.get(1).getLng() * 1E6));
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.FROYO) {
mapController.animateTo(geoPoint, new Message());
}
}
public MapView getMapView() {
return this.mapView;
}
Вот код для моего класса CustomItemizedOverlay (это может вас испугать)
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>
implements OnKeyListener {
private final Bitmap bitMap;
private ViewFlipper viewFlipper;
private final ArrayList<OverlayItem> overLayItems = new ArrayList<OverlayItem>();
private final GoogleMapsActivity googleMapsActivity;
public int selectedIndex = -1;
private View view = null;
private RelativeLayout relativeLayout = null;
private static WebView webView;
public CustomItemizedOverlay(GoogleMapsActivity googleMapsActivity) {
super(boundCenterBottom(googleMapsActivity.getResources().getDrawable(
R.drawable.pingreen)));
this.googleMapsActivity = googleMapsActivity;
bitMap = BitmapFactory.decodeResource(
googleMapsActivity.getResources(), R.drawable.pingreen);
populate();
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
if (overLayItems == null || overLayItems.isEmpty()) {
return false;
}
return super.draw(canvas, mapView, false, when);
}
public void addOverlay(OverlayItem overlay) {
overLayItems.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int index) {
return overLayItems.get(index);
}
@Override
public int size() {
return overLayItems.size();
}
public void refresh() {
populate();
}
public void clear() {
overLayItems.clear();
resetLastFocuesIndex();
}
@Override
protected boolean onTap(final int index) {
googleMapsActivity.getMapView().setOnTouchListener(
new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (!overLayItems.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
}
}
googleMapsActivity.getMapView().invalidate();
return false;
}
});
if (view != null) {
view.setVisibility(View.GONE);
googleMapsActivity.getMapView().removeView(view);
googleMapsActivity.getMapView().invalidate();
view = null;
}
view = googleMapsActivity.getLayoutInflater().inflate(
R.layout.popupwindow, null);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.popupwindow);
ImageView image = (ImageView) view.findViewById(R.id.imageview);
TextView text = (TextView) view.findViewById(R.id.text);
text.setText(overLayItems.get(index).getTitle());
if (overLayItems.get(index).getTitle() != null
&& overLayItems.get(index).getTitle().equals("Me") == false) {
image.setImageResource(R.drawable.wwwpd);
}
Projection projection = googleMapsActivity.getMapView().getProjection();
Point point = new Point();
projection.toPixels(overLayItems.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -bitMap.getHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, overLayItems.get(index)
.getPoint(), x, y, MapView.LayoutParams.BOTTOM_CENTER);
googleMapsActivity.getMapView().removeView(view);
googleMapsActivity.getMapView().invalidate();
googleMapsActivity.getMapView().addView(view, lp);
googleMapsActivity.getMapView().invalidate();
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (Common.haveNetworkConnection(googleMapsActivity)) {
googleMapsActivity.getMapView().removeView(view);
googleMapsActivity.getMapView().invalidate();
view = null;
String desription = googleMapsActivity.getMarkersList()
.get(index).getDescription();
String str1 = desription.substring(
desription.indexOf("\"") + 1, desription.length());
String str2 = str1.substring(0, str1.indexOf("\""));
viewFlipper = (ViewFlipper) googleMapsActivity
.findViewById(R.id.flipper);
viewFlipper.setDisplayedChild(2);
relativeLayout = (RelativeLayout) googleMapsActivity
.findViewById(R.id.relativeLayout);
Button button = new Button(googleMapsActivity);
button.setText("Back");
button.setId(1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(params1);
button.setVisibility(View.GONE);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
viewFlipper.setDisplayedChild(0);
}
});
relativeLayout.addView(button);
webView = new WebView((Context) (googleMapsActivity
.getParent() == null ? this : googleMapsActivity
.getParent()));
webView.setId(2);
webView.getSettings().setDefaultZoom(ZoomDensity.MEDIUM);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
// webView.getSettings()
// .setUserAgentString(
// "Mozilla/5.0 (Linux; U; Android 2.3.3; en-au; GT-I9100 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.12011-10-16 20:22:55");
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
webView.setLayoutParams(params2);
params2.addRule(RelativeLayout.BELOW, 1);
relativeLayout.addView(webView);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
super.onPageStarted(view, url, favicon);
viewFlipper.setDisplayedChild(2);
}
@Override
public void onPageFinished(WebView view, String url) {
viewFlipper.setDisplayedChild(1);
webView.requestFocus(View.FOCUS_DOWN);
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
}
@Override
public void onReceivedError(WebView view,
int errorCode, String description,
String failingUrl) {
super.onReceivedError(view, errorCode, description,
failingUrl);
viewFlipper.setDisplayedChild(0);
}
});
webView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode,
KeyEvent keyEvent) {
if ((keyCode == KeyEvent.KEYCODE_BACK)
&& webView.canGoBack()) {
webView.goBack();
return true;
}
googleMapsActivity.finish();
return true;
}
});
webView.loadUrl(str2);
}
}
});
selectedIndex = index;
return true;
}
public void resetLastFocuesIndex() {
setLastFocusedIndex(-1);
selectedIndex = -1;
}
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
viewFlipper.showPrevious();
return true;
}
return true;
}
}