Android 使用Google地图
2020-03-22 本文已影响0人
itfitness
目录
前言
最近的一个项目需要使用Google地图,因此总结了下,我在项目中使用的效果。
效果展示
●全屏显示
●列表显示
实现步骤
首先需要去Google地图开放平台申请ApiKeyGoogle地图开放平台,然后在Android Studio中新建项目,这样的话就会自动集成Google地图的依赖。
创建完项目后我们需要在AndroidManifest.xml文件中配置ApiKey
这时运行项目后就会展示出Google地图了(需要找一个带有Google服务的手机和解决获取Google数据的问题)
默认地图是搭载在fragment上的
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:cameraZoom="18"//缩放级别
map:mapType="normal"//地图类型(具体可以查看Google官方文档)
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
地图的初始化代码如下:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
通过getMapAsync方法传入回调接口获取初始化成功后的GoogleMap对象
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
然后我们在onMapReady回调方法中可以对地图进行操作,如效果图所示的添加标记(具体的操作跟高德地图的方法几乎一样):
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(36.69314, 117.10170);//经纬度坐标
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ic_dog);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(bitmapDescriptor));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
另外我们也可以使用MapView来展示地图:
<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:cameraZoom="18"
map:mapType="normal"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
初始化方法如下:
MapView mMap = (MapView) findViewById(R.id.map);
mMap.onCreate(savedInstanceState);
mMap.onResume();
try {
MapsInitializer.initialize(this);
} catch (Exception e) {
e.printStackTrace();
}
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS != errorCode) {
GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
} else {
mMap.getMapAsync(this);//这里的接口回调跟使用fragment搭载地图时用的一样
}
另外由于我的项目中需要列表也展示地图,因此也总结了下列表使用地图的方法,大体跟全屏显示一样。
//这段代码用在对列表控件进行添加数据的时候使用
@Override
protected void convert(BaseViewHolder helper, String item) {
MapView itemMap = helper.getView(R.id.item_map);
itemMap.onCreate(new Bundle());
itemMap.onResume();
try {
MapsInitializer.initialize(mContext);
} catch (Exception e) {
Log.e("出错",e.getMessage());
}
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
if (ConnectionResult.SUCCESS == errorCode) {
itemMap.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
double lat = 36.69314;
double lng = 117.10170;
LatLng appointLoc = new LatLng(lat, lng);
// 移动地图到指定经度的位置
googleMap.moveCamera(CameraUpdateFactory.newLatLng(appointLoc));
//添加标记到指定经纬度
googleMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Marker")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_dog)));
}
});
}else {
Log.e("出错",errorCode+"");
}
}