Android Google Map
Android在我們的應(yīng)用程序中提供了集成Google地圖的功能。愛掏網(wǎng) - it200.comGoogle地圖顯示當(dāng)前位置、導(dǎo)航位置方向、搜索位置等等。愛掏網(wǎng) - it200.com我們還可以根據(jù)需求自定義Google地圖。愛掏網(wǎng) - it200.com
有四種不同類型的Google地圖,還可以選擇不顯示地圖。愛掏網(wǎng) - it200.com它們在地圖上提供不同的視圖。愛掏網(wǎng) - it200.com這些地圖如下:
- 正常地圖: 此類型的地圖顯示一般道路地圖、河流等自然特征以及人造特征。愛掏網(wǎng) - it200.com
- 混合地圖: 此類型的地圖將衛(wèi)星照片數(shù)據(jù)與一般道路地圖結(jié)合顯示。愛掏網(wǎng) - it200.com它還顯示道路和特征標簽。愛掏網(wǎng) - it200.com
- 衛(wèi)星地圖: 衛(wèi)星類型顯示衛(wèi)星照片數(shù)據(jù),但不顯示道路和特征標簽。愛掏網(wǎng) - it200.com
- 地形地圖: 這種類型顯示照片數(shù)據(jù)。愛掏網(wǎng) - it200.com包括顏色、等高線和標簽以及透視陰影。愛掏網(wǎng) - it200.com
- 無地圖: 此類型顯示一個空的網(wǎng)格,沒有加載任何瓷磚。愛掏網(wǎng) - it200.com
不同類型地圖的語法
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
谷歌地圖的方法
谷歌地圖API提供了幾種方法,可以幫助定制谷歌地圖。愛掏網(wǎng) - it200.com這些方法如下:
方法 | 描述 |
---|---|
addCircle(CircleOptions options) | 此方法向地圖上添加圓形。愛掏網(wǎng) - it200.com |
addPolygon(PolygonOptions options) | 此方法向地圖上添加多邊形。愛掏網(wǎng) - it200.com |
addTileOverlay(TileOverlayOptions options) | 此方法向地圖上添加瓦片覆蓋圖層。愛掏網(wǎng) - it200.com |
animateCamera(CameraUpdate update) | 此方法根據(jù)更新內(nèi)容以動畫方式移動地圖。愛掏網(wǎng) - it200.com |
clear() | 此方法從地圖上移除所有內(nèi)容。愛掏網(wǎng) - it200.com |
getMyLocation() | 此方法返回當(dāng)前顯示的用戶位置。愛掏網(wǎng) - it200.com |
moveCamera(CameraUpdate update) | 此方法根據(jù)更新內(nèi)容重新定位地圖相機。愛掏網(wǎng) - it200.com |
setTrafficEnabled(boolean enabled) | 此方法打開或關(guān)閉交通圖層。愛掏網(wǎng) - it200.com |
snapshot(GoogleMap.SnapshotReadyCallback callback) | 此方法對地圖進行快照。愛掏網(wǎng) - it200.com |
stopAnimation() | 此方法停止地圖相機動畫(如果有進行中的動畫)。愛掏網(wǎng) - it200.com |
谷歌地圖示例
讓我們創(chuàng)建一個集成到我們的應(yīng)用程序中的谷歌地圖的示例。愛掏網(wǎng) - it200.com為此,我們選擇谷歌地圖活動。愛掏網(wǎng) - it200.com
將來自google_map_api.xml文件的URL復(fù)制以生成Google地圖密鑰。愛掏網(wǎng) - it200.com
將復(fù)制的URL粘貼到瀏覽器中。愛掏網(wǎng) - it200.com它將打開以下頁面。愛掏網(wǎng) - it200.com
點擊“Create API key”來生成API密鑰。愛掏網(wǎng) - it200.com
點擊Create API key后,將生成我們的API密鑰,顯示以下屏幕。愛掏網(wǎng) - it200.com
復(fù)制此生成的 API 密鑰到我們的 google_map_api.xml 文件中
activity_maps.xml
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.mapexample.MapsActivity" />
MapsActivity.java
在MapsActivity.java類中獲取GoogleMap對象,我們需要實現(xiàn)OnMapReadyCallback接口并重寫onMapReady()回調(diào)方法。愛掏網(wǎng) - it200.com
package example.com.mapexample;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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 MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
所需權(quán)限
在AndroidManifest.xml文件中添加以下用戶權(quán)限。愛掏網(wǎng) - it200.com
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com.mapexample">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradel
將以下依賴項添加到 build.gradel 文件中。愛掏網(wǎng) - it200.com
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-maps:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
輸出