mars老师的googleMap示例(二)

manifest.xml文件

package="test.map03"
android:versionCode="1"
android:versionName="1.0">




android:label="@string/app_name">









2、布局文件

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/mapViewId"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0C7R0KjQ5CR8hdVNpQlvJaRtn8wuek7GhYyY9Ng"
/>


3、FirstOverlay.java
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class FirstOverlay extends ItemizedOverlay{
private Context context;
private ArrayList overlayItems = new ArrayList();
public FirstOverlay(Drawable defaultMarker,Context context)
{
super(boundCenterBottom(defaultMarker));
this.context = context;
}
public FirstOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
//用于将生成好的OverlayItem对象添加到List当中
public void addOverlayItem(OverlayItem overlayItem)
{
overlayItems.add(overlayItem);
populate();
}
//用于创建一个OverlayItem对象
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return overlayItems.get(i);
}
//返回当前Overlay当中所包含的OverlayItem对象的数量
@Override
public int size() {
// TODO Auto-generated method stub
return overlayItems.size();
}
@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
OverlayItem item = overlayItems.get(index);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(item.getTitle());
builder.setMessage(item.getSnippet());
Dialog dialog = builder.create();
dialog.show();
return true;
}

}

4、MainActivity.java
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

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

public class TestMap03Activity 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.mapViewId);
mapView.setBuiltInZoomControls(true);

//调用mapView对象的getOverLays()方法,用于得到所有的图层对象
List mapOverlays = mapView.getOverlays();
//生成Drawable对象
Drawable drawable = getResources().getDrawable(R.drawable.icon);
FirstOverlay firstOverlay = new FirstOverlay(drawable,this);
//创建一个GeoPoint对象,用于通过经纬度,指定地图上的一个点
GeoPoint point = new GeoPoint(19240000,-99120000);
//创建一个OverLayItem对象
OverlayItem overlayItem = new OverlayItem(point,"Hola,Mundo!","I'm in Mexico City!");
firstOverlay.addOverlayItem(overlayItem);
mapOverlays.add(firstOverlay);
}

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


执行结果
[img][/img]

你可能感兴趣的:(android)