今天参照Android Api里的Hello Views来学习GMap,不想,Api里好多错误,囧
so,我在这里贴出正确的代码,供大家参考
TODO(1):显示Map(Creating a Map Activity)
step1:创建一个新的工程
step2:由于Maps library不是标准库里的东东,SO,要在AndroidManifest.xml文件中加上一个library:
<uses-library android:name="com.google.android.maps" />
step3:Map功能需要有网络连接,SO,权限不能少:
<uses-permission android:name="android.permission.INTERNET" />
step4:隐藏titleBar(随意):
<activity android:name=".GMapTest" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
step5:在res/layout/main.xml里加一个MapView控件:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="Your Maps API Key goes here"
/>
PS:关于如何得到Key,请参看
http://www.blogjava.net/crazycoding/archive/2011/10/11/360937.html
step6:打开自动创建的Activity (GMap.java)文件
修改之,使其继承自MapActivity
public class GMapTest extends MapActivity {
step7:实现相应方法:
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
step8:完善onCreate方法:
setContentView(R.layout.main);
此时,地图已经可以显示出来了,不过,我们可以给它加上缩放按钮:
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
哦了。
现整理相关文件完整代码如下:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yinger"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GMapTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name = "com.google.android.maps"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
main.xml:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled = "true"
android:clickable = "true"
android:apiKey="055ZV_999VhK6Zcr-8P7tyGzeRwTnWV_JET9kKg"
/>
</LinearLayout>
GMapTest.java:
package com.yinger;
import com.google.android.maps.MapActivity;
import android.os.Bundle;
/**
* HelloWorld Google Map
*
* @author Ying_er
* @Email melody.crazycoding@gmail.com
* @time 2011/10/11 14:12:15
* @version 1.00
*/
public class GMapTest extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
TODO(2):在Map上显示标记,效果如图:
首先,我先简单的说一嘴在Map上显示标记的基本原理:一个图层显示地图,一个图层显示标记,然后两个图层罗列。
SO,也就是Adding Overlay Items
(假设已经按TODO(1)的步骤完成了相应代码)
step1:创建一个新的Java类:HelloItemizedOverlay,让他继承ItemizedOverlay<OverlayItem>:
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
step2:创建一个List对象,存储该图层中所有的标记对象
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
step3:构造方法:
/**
* 参数用于指定显示标记的默认图片
* @param arg0
*/
public HelloItemizedOverlay(Drawable arg0) {
super(boundCenterBottom(arg0));
// TODO Auto-generated constructor stub
}
step4:定义方法,将生成好的OverlayItem对象添加到List当中
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
step5:执行populate方法时,会执行createItem方法,来创建一个OverlayItem对象
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
step6:重写方法,返回当前Overlay中的OverlayItem对象个数
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
step7:为了能相应点击事件,需要一个Context的引用,SO,增加如下构造函数:
public HelloItemizedOverlay(Drawable arg0, Context context) {
super(boundCenterBottom(arg0));
// TODO Auto-generated constructor stub
this.context = context;
}
当用户点击标记时所执行的操作:
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
step8:回到MapActivity的onCreate方法
得到所有图层对象:
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, this);
创建GeoPoint对象,通过经纬度,指定地图上的一个点
GeoPoint point = new GeoPoint(19240001, -99120000);
创建一个OverlayItem对象
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
"I'm in Mexico City!");
将创建好的OverlayItem对象放到HelloItemizedOverlay当中
itemizedoverlay.addOverlay(overlayitem);
将HelloItemizedOverlay放到mapOverlays当中
mapOverlays.add(itemizedoverlay);
当然,也可以多创建几个标记。
部分文件完整代码:
MarkPoint.java
package com.yinger;
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;
/**
* 显示标记
*
* @author Ying_er
* @Email melody.crazycoding@gmail.com
* @time 2011/10/12 15:23:42
* @version 1.00
*/
public class MarkPoint extends MapActivity {
MapView mapView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
/**
* 得到所有图层对象
*/
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
drawable, this);
/**
* 创建GeoPoint对象,通过经纬度,指定地图上的一个点
*/
GeoPoint point = new GeoPoint(19240001, -99120000);
/**
* 创建一个OverlayItem对象
*/
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
"I'm in Mexico City!");
GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
/**
* 将创建好的OverlayItem对象放到HelloItemizedOverlay当中
*/
itemizedoverlay.addOverlay(overlayitem);
itemizedoverlay.addOverlay(overlayitem2);
/**
* 将HelloItemizedOverlay放到mapOverlays当中
*/
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
HelloItemizedOverlay.java:
package com.yinger;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
/**
* 在MapView之上,创建一个图层(OverlayItem) 生成该类对象,并将该对象添加到MapView.getOverlays()里
* 一个OverlayItem对象就代表了一个在地图上显示的标记
*
* @author Ying_er
* @Email melody.crazycoding@gmail.com
* @time 2011/10/12 14:53:17
* @version 1.00
*/
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
/**
* 创建一个List对象,存储该图层中所有的标记对象
*/
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context;
/**
* 参数用于指定显示标记的默认图片
*
* @param arg0
*/
public HelloItemizedOverlay(Drawable arg0) {
super(boundCenterBottom(arg0));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable arg0, Context context) {
super(boundCenterBottom(arg0));
// TODO Auto-generated constructor stub
this.context = context;
}
/**
* 创建一个OverlayItem对象
*/
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
/**
* 返回当前Overlay中的OverlayItem对象个数
*/
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
/**
* 将生成好的OverlayItem对象添加到List当中
*
* @param overlay
*/
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
/**
* 当用户点击标记时所执行的操作
*/
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
posted on 2011-10-12 15:47
Ying-er 阅读(1914)
评论(1) 编辑 收藏 所属分类:
Google Map 、
Android