OUTLINE
• 課程目標與進度
• Google map API key
• Google map API 介紹與使用
• Google map API 事件與
FrameLayout
• Google map API UI
• Google map API 繪製線
• 目前問題彙整
• 實作:Google map 與GPS感測
器互動
• Q&A
22Presented By: Duran Hsieh
ANDROID GOOGLE MAP API 介紹與使用
• 常用語法
• 經緯度(LatLng)
• 標記(MarkerOptions)
• 加入地圖
new MarkerOptions()
.position(latlng)
.title("標題")
.snippet("描述")
.draggable(true);
LatLng latlng = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(latlng));
23Presented By: Duran Hsieh
ANDROID GOOGLE MAP API 介紹與使用
• 更多標記(MarkerOptions)
MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.common_full_open_on_phone))
.anchor(0.0f, 1.0f)
.position(new LatLng(41.889, -87.622));
24Presented By: Duran Hsieh
ANDROID GOOGLE MAP API 介紹與使用
• 移動位置
• 縮放
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-34, 151)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new LatLng(-34, 151),13));
25Presented By: Duran Hsieh
ANDROID GOOGLE MAP API 介紹與使用
• 地圖類型
• GoogleMap.MAP_TYPE_SATELLITE
• GoogleMap.MAP_TYPE_NONE
• GoogleMap.MAP_TYPE_NORMAL
• GoogleMap.MAP_TYPE_HYBRID
• GoogleMap.MAP_TYPE_TERRAIN
• 設定地圖類型
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
26Presented By: Duran Hsieh
ANDROID GOOGLE MAP API 介紹與使用
• 開始撰寫第一個 map activity
• 加入套件 (com.google.android.gms:play-services-maps)
• 加入google map api key
• 加入 fragment (XML)
• 加入程式
27Presented By: Duran Hsieh
ANDROID GOOGLE MAP API 介紹與使用
• 加入套件
• 方法1:file -> project structure
28Presented By: Duran Hsieh
ANDROID GOOGLE MAP API 介紹與使用
• app - > dependencies
• 找到 com.google.android.gms:play-services-maps:10.2.1
29Presented By: Duran Hsieh
ANDROID GOOGLE MAP API 介紹與使用
• 方法2:
• build.gradle - > dependencies
• 於dependencies 內加上 com.google.android.gms:play-
services-maps:10.2.1
45Presented By: Duran Hsieh
GOOGLE MAP API 繪製線
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng taiwan = new LatLng(23.97, 120.98);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(taiwan, 8));
mMutablePolyline = mMap.addPolyline(new PolylineOptions()
.add(A, B, C, D)
.width(5)
.color(Color.BLUE)
.clickable(true));
mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
@Override
public void onPolylineClick(Polyline polyline) {
polyline.setColor(polyline.getColor() ^ 0x00ffffff);
}
});
}
Step 1. 加入屬性(點、線寬、顏色、點擊)
Step 1. 加入點擊事件
46Presented By: Duran Hsieh
GOOGLE MAP API 繪製線
• AndroidGoogleMapPolylineExample:
• https://github.com/matsurigoto/AndroidGoogleMapPolyli
neExample.git
49Presented By: Duran Hsieh
目前問題彙整
• 如何找 DEBUG ?
Step 1.
Set Break point
Step 2.Debug Step 3.Next Step
50Presented By: Duran Hsieh
目前問題彙整
• 問題一 Error:Tag <uses-permission> attribute name
has invalid character ' '.
51Presented By: Duran Hsieh
目前問題彙整
• 問題二 Attempt to invoke virtual method ‘android.view.Window$Callback
android.view.Window.getCallback()' on a null object reference
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));}
Google map 官方有提供範例可以參考https://github.com/googlemaps/android-samples
Note: Insert your picture by clicking on the Picture Place Holder Icon, then send it back!