实现HelloGoogleMaps这个例子比较麻烦了点,下面一步步说说。
1. 按照google文档建立项目,建立项目用的build target要用Google APIs,AVD设备也要用Google APIs,否则程序无法显示入口图标,无法运行等。
2. 如何获得下面的apikey。
android:apiKey="Your Maps API Key goes here"
a>在DOS窗口用如下命令获得MD5:
C:\Program Files\Java\jre7\bin>keytool -v -list -alias androiddebugkey -keystore
"C:\Users\t\.android\debug.keystore" -storepass android -keypass android
注意:
在jdk7版本以上必须加-v,否则得到是
20位的SHA1的值
,注册需要的是16位的MD5.别搞错奥!
b> 在 https://developers.google.com/android/maps-api-signup?hl=zh-CN 页面输入上一步得到的MD5, 选中“同意协议”,当然还要有个google帐号,点击生成进入下就一个页面就会显示生成apikey。
如果上述网址无法正常显示,https://developers.google.com/android/maps-api-signup?hl=zh-TW这个可以备用。如果这个还不行,那就自己找“tizi”吧。
如果错误的将SHA1输入,进入页面显示乱码,意思就是你输入的MD5不合法。
c> 将上一步得到的apikey拷贝到main.xml对应的位置:
android:apiKey="0eIPNkAcKF3xgG26zq8HXA4LgvMpSKe2vvs8QJQ"
如果你的MD5不是通过keytool工具得来,而是自己编了一个MD5骗google,那么对不起,你的应用仍然无法显示地图,只是白板一块。
如果apikey对了,进入程序,点击放大按钮就会显示地图了。
d>建立类 ItemizedOverlay
。如下代码放到HelloGoogleMaps::onCreate()程序后面就OK,其他基本按照文档就可以。
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
3. 附上代码:
main.xml
<?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="0eIPNkAcKF3xgG26zq8HXA4LgvMpSKe2vvs8QJQ"
/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.HelloGoogleMaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HelloGoogleMaps"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<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>
</manifest>
HelloGoogleMaps.java
package com.example.HelloGoogleMaps;
import java.util.List;
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;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class HelloGoogleMaps 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.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000,-99120000);
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!");
itemizedoverlay.addOverlay(overlayitem2);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
HelloItemizedOverlay.java
package com.example.HelloGoogleMaps;
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;
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
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(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
private Context mContext;
}