最近项目中需要接入Google地图,遇到很多问题,现在记录下来,供以后参考。
1. 登录Google API Console,注册Android API密钥(需要翻墙 ~_~)
得到API KEY:
2. 下载Google Play 服务 SDK
即google-play-services_lib(里面包含google-play-services.jar),用SDK Manager没找到,在GitHub上下了一个:
3. 地图接入,添加marker
配置AndroidManifest.xml清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mark.maptrackdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<uses-library android:name="com.google.android.maps" android:required="true" />
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBmemD8LhxHNbI6jG2Fttmormx-PtCGQrA" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
定义XML 布局文件,位于res/layout/activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
定义地图 Activity 的 Java 文件名为 MainActivity.java:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
// Googleplex经纬度
private final LatLng googleLatLng = new LatLng(37.422277, -122.084058);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(onMapReadyCallback);
}
protected OnMapReadyCallback onMapReadyCallback = new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
//添加标记
googleMap.addMarker(new MarkerOptions().position(googleLatLng)
.title("Marker in Googleplex"));
//移动摄像头
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(googleLatLng, 13));
}
};
}
在具有Google服务的手机上运行,国内安卓系统会报错:Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY。
运行成功可以看到如下效果(需要翻墙,不然一片空白):