Android Programming วรเศรษฐ ส วรรณ ก uuriter@yahoo.com http://bit.ly/wannikacademy 1
Google Map API v2 2
Preparation SDK Manager Google Play Services AVD Google API >= 4.2.2 [http://bit.ly/1hedxwm] https://developers.google.com/maps/documentation/android/start 3
API Key 4
API Key 5
API Key 6
API Key 7
API Key 8
API Key : ADT Preferences 9
API Key 10
API Key 11
Manifest <manifest> <uses-feature android:glesversion="0x00020000" android:required="true" /> <uses-sdk android:minsdkversion="14" android:targetsdkversion="14" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" /> <application>... <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.maps.v2.api_key" android:value="---------apikey-----------" /> </application> </manifest> 12
Layout <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:name="com.google.android.gms.maps.mapfragment" android:layout_width="match_parent" android:layout_height="match_parent" /> 13
Activity package com.example.testmap; import android.app.activity; import android.os.bundle; public class MainActivity extends Activity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } } 14
Normal 15
Map Type GoogleMap map = ((MapFragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); map.setmaptype(googlemap.map_type_hybrid); https://developers.google.com/maps/documentation/android/map 16
Hybrid 17
Initial Location latitude north-south 90 to -90 longitude east-west 180 to -180 GoogleMap map = ((MapFragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap();; map.movecamera(cameraupdatefactory.newlatlng(new LatLng(13.8484877,100.5715896))); map.movecamera(cameraupdatefactory.zoomto(16.4f)); https://developers.google.com/maps/documentation/android/map 18
Initial Location 19
Location <uses-permission android:name="android.permission.access_fine_location" /> GPS, cell tower, WIFI implies <uses-permission android:name="android.permission.access_coarse_location" /> cell tower, WIFI 20
Current Location import com.google.android.gms.common.googleplayservicesclient.connectioncallbacks; import com.google.android.gms.common.googleplayservicesclient.onconnectionfailedlistener; public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener { LocationClient lc; GoogleMap map; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lc = new LocationClient(this, this, this); map = ((MapFragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); } 21
Current Location @Override protected void onstart() { super.onstart(); lc.connect(); } @Override protected void onstop() { lc.disconnect(); super.onstop(); } 22
Current Location @Override public void onconnectionfailed(connectionresult arg0) { } @Override public void onconnected(bundle arg0) { Log.i("map", "connect"); Location loc = lc.getlastlocation(); LatLng latlng = new LatLng(loc.getLatitude(), loc.getlongitude()); map.animatecamera(cameraupdatefactory.newlatlng(latlng)); } } @Override public void ondisconnected() { Log.i("map", "disconnect"); } 23
Current Location 24
Current Location map.setmylocationenabled(true); 25
Location Listener @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); locationclient = new LocationClient(this, this, this); map = ((MapFragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); locationrequest = LocationRequest.create(); locationrequest.setpriority(locationrequest.priority_high_accuracy); locationrequest.setinterval(5); locationrequest.setfastestinterval(1); } 26
Location Listener @Override public void onconnected(bundle arg0) { locationclient.requestlocationupdates(locationrequest, new LocationListener() { @Override public void onlocationchanged(location loc) { LatLng latlng = new LatLng(loc.getLatitude(), loc.getlongitude()); map.animatecamera(cameraupdatefactory.newlatlng(latlng)); } }); } 27
Click Listener map.setonmapclicklistener(new OnMapClickListener() { @Override public void onmapclick(latlng latlng) { Log.i("map", latlng.latitude + " " + latlng.longitude); } }); long click listener 28
Click Listener map.setonmapclicklistener(new OnMapClickListener() { @Override public void onmapclick(latlng latlng) { Log.i("map", latlng.latitude + " " + latlng.longitude); } }); long click listener 29
Marker public class MainActivity extends Activity { GoogleMap map; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } map = ((MapFragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); map.addmarker(new MarkerOptions().position(new LatLng(0, 0)).title( "Center of the World")); } 30
Marker 31
Marker public class MainActivity extends Activity { GoogleMap map; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } map = ((MapFragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); map.addmarker(new MarkerOptions().position(new LatLng(0, 0)).title( "Center of the World")); } https://developers.google.com/maps/documentation/android/marker 32
Marker 33
Marker 34
Marker Marker marker = map.addmarker(new MarkerOptions().position(new LatLng(0, 0)).title("Center of the World")); // try snippet marker.showinfowindow(); // map.setonmarkerclicklistener https://developers.google.com/maps/documentation/android/infowindows 35
Shape PolylineOptions rectoptions = new PolylineOptions().add(new LatLng(0, 0)).add(new LatLng(30, 10)).add(new LatLng(35, 20)).add(new LatLng(60, 30)).add(new LatLng(0, 0)).width(5).color(Color.BLUE).geodesic(true); map.addpolyline(rectoptions); A geodesic is the shortest path between two points on the Earth's surface. The geodesic curve is constructed assuming the Earth is a sphere. http://bit.ly/1mz3pk5 https://developers.google.com/maps/documentation/android/shapes 36
Shape 37
CRUD Marker Map Long Click add marker (color, alpha), ask for a title, log position Click marker show info window Click info window pop up menu => 38