Android开发要连接GoogelMaps使用MapView时需要,先从Google网站申请一组经过验证的Maps API Key授权码,这个在网上很多。
[代码] [Java]代码
package com.android.antking.map;
import com.google.android.maps.MapActivity;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.os.Bundle;
public class MyMain extends MapActivity {
//放大倍数
static final int INITIAL_ZOOM_LEVEL=12;
//中心点
static final int INITIAL_LATITUDE=123465465;
static final int INITIAL_LONGITUDR=15634646;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapView);
//enable to change the size of the map
mapView.setBuiltInZoomControls(true);
//设置Zoom 大小和地图的中心点
MapController mc = mapView.getController();
mc.setZoom(INITIAL_ZOOM_LEVEL);
mc.setCenter(new GeoPoint(INITIAL_LATITUDE,INITIAL_LONGITUDR));
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
[代码] [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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0FZLYf-YM4SRrJrJum55MeeaO4Gd_IitVFmtUeA"/>这个是自己开发的google mapApi 密钥
</LinearLayout>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
[代码] 在这里加上google的地图包,还有Internet权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.antking.map"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyMain"
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"></uses-library>
</application>
</manifest>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
1.创建工程,注意SDK旋转为"Goolge APIs”
2.修改AndroidManifest.xml文件
由于使用Google Map API,所以必须添加<uses-library android:name="com.google.android.maps" />
由于需要从网络获取地图数据,所以需要访问网络的权限<uses-permission android:name="android.permission.INTERNET"/>
可能还需要添加其他权限。
3.创建MapView
4.实现MapActivity
5.MapController的使用
6.Ovelay的使用