`
chengfengyang
  • 浏览: 20978 次
社区版块
存档分类
最新评论

mars老师的googleMap示例(二)

阅读更多
manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="test.map03"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
     <uses-library android:name="com.google.android.maps" />
        <activity android:name=".TestMap03Activity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>


2、布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <com.google.android.maps.MapView
                 android:id="@+id/mapViewId"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:enabled="true"
                 android:clickable="true"
                 android:apiKey="0C7R0KjQ5CR8hdVNpQlvJaRtn8wuek7GhYyY9Ng"
                 />
</LinearLayout>

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<OverlayItem>{
     private Context context;
     private ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>();
	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<Overlay> 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]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics