ANDROID_MARS学习笔记_S03_008_GOOGLEMAP2

一、简介

二、代码
1.xml
(1)main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <com.google.android.maps.MapView
 8         android:id="@+id/mapViewId"
 9         android:layout_width="fill_parent"
10         android:layout_height="fill_parent"
11         android:enabled="true"
12         android:clickable="true"
13         android:apiKey="0pkT0EYxPi2VZ5beDaJ0g08aCtWGmKTFnOvj6iw"
14         />
15 </LinearLayout>

 

(2)AndroidManifest.xml

1 <uses-permission android:name="android.permission.INTERNET" />

 

2.java
(1)MainActivity.java

 1 package com.se7en;
 2 
 3 import java.util.List;
 4 
 5 import android.graphics.drawable.Drawable;
 6 import android.os.Bundle;
 7 
 8 import com.google.android.maps.GeoPoint;
 9 import com.google.android.maps.MapActivity;
10 import com.google.android.maps.MapView;
11 import com.google.android.maps.Overlay;
12 import com.google.android.maps.OverlayItem;
13 
14 public class MainActivity extends MapActivity {
15     /** Called when the activity is first created. */
16     @Override
17     public void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.main);
20         MapView mapView = (MapView)findViewById(R.id.mapViewId);
21         //设置mapView显示用于缩放的工具条
22         mapView.setBuiltInZoomControls(true);
23         
24         Drawable drawable = getResources().getDrawable(R.drawable.index);
25         FirstOverlay firstOverlay = new FirstOverlay(drawable,this);
26       //创建一个GeoPoint对象,用于通过经纬度指定地图上的一个点
27         GeoPoint point = new GeoPoint(19240000,-99120000);
28         //创建一个OverlayItem对象,代表图层上的一个标记
29         OverlayItem overlayItem = new OverlayItem(point,"hola,mundo!","I'm in china!");
30         firstOverlay.addOverlay(overlayItem);
31 
32         //调用MapView对象的getOverlays(),得到所有的图层对象
33         List<Overlay> mapOverlays = mapView.getOverlays();
34         mapOverlays.add(firstOverlay);
35     }
36 
37     @Override
38     protected boolean isRouteDisplayed() {
39         // TODO Auto-generated method stub
40         return false;
41     }
42 }

 

(2)FirstOverlay.java

 

 1 package com.se7en;
 2 
 3 import java.util.ArrayList;
 4 
 5 import android.app.AlertDialog;
 6 import android.app.Dialog;
 7 import android.content.Context;
 8 import android.graphics.drawable.Drawable;
 9 
10 import com.google.android.maps.ItemizedOverlay;
11 import com.google.android.maps.OverlayItem;
12 
13 /**
14  * 在MapView之上创建一个图层,需要创建一个类,实现Overlay,并生成该类的对象,然后将该对象添加到MapView.getOverlays()中;
15  * 一个overlayItem对象就代表了一个在地图上显示的标记
16  * @author se7en
17  *
18  */
19 public class FirstOverlay extends ItemizedOverlay<OverlayItem>{
20     //创建一个List对象,用于持有该图层当中所有的标记对象
21     private ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>();
22     private Context context;
23     
24     public FirstOverlay(Drawable defaultMaker) {
25         super(boundCenter(defaultMaker));
26     }
27     
28     /**
29      * @param defaultMaker 指定标记所使用的默认图片
30      * @param context
31      */
32     public FirstOverlay(Drawable defaultMaker,Context context) {
33         //boundCenter(defaultMaker):将要标记的经纬度点放在defaultMaker的正下方
34         super(boundCenter(defaultMaker));
35         this.context = context;
36     }
37     
38     //用于将生成好的overlayItem对象添加到List当中
39     public void addOverlay(OverlayItem overlayItem){
40         overlayItems.add(overlayItem);
41         //为新添加进来的overlayItem统一执行相关的操作(具体待考证。。。)
42         populate();
43     }
44     
45     //用于创建一个OverlayItem对象
46     @Override
47     protected OverlayItem createItem(int i) {
48         // TODO Auto-generated method stub
49         return overlayItems.get(i);
50     }
51 
52     //返回当前的Overlay当中所包含的overlayItem对象
53     @Override
54     public int size() {
55         // TODO Auto-generated method stub
56         return overlayItems.size();
57     }
58     
59     //当用户点击标记执行的操作
60     @Override
61     public boolean onTap(int index) {
62         OverlayItem item = overlayItems.get(index);
63         AlertDialog.Builder builder = new AlertDialog.Builder(context);
64         builder.setTitle(item.getTitle());
65         builder.setMessage(item.getSnippet());
66         Dialog dialog = builder.create();
67         dialog.show();
68         return true;
69     }
70 
71 }

 

 

 

 

 

你可能感兴趣的:(ANDROID_MARS学习笔记_S03_008_GOOGLEMAP2)