Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
2
Google Map API v2
3
Preparation● SDK Manager
– Google Play Services● AVD
– Google API >= 4.2.2 [http://bit.ly/1hEdxWm]
https://developers.google.com/maps/documentation/android/start
4
API Key
5
API Key
6
API Key
7
API Key
8
API Key
9
API Key : ADT Preferences
10
API Key
11
API Key
12
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>
13
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" />
14
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); }}
15
Normal
16
Map TypeGoogleMap map = ((MapFragment) getFragmentManager() .findFragmentById(R.id.map)).getMap();map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
https://developers.google.com/maps/documentation/android/map
17
Hybrid
18
Initial Location
https://developers.google.com/maps/documentation/android/map
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));
● latitude– north-south 90 to -90
● longitude– east-west 180 to -180
19
Initial Location
20
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
21
Current Locationimport 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(); }
22
Current Location
@Override protected void onStart() { super.onStart(); lc.connect(); }
@Override protected void onStop() { lc.disconnect(); super.onStop(); }
23
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"); }}
24
Current Location
25
Current Location
map.setMyLocationEnabled(true);
26
Location Listener
@Overrideprotected 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); }
27
Location Listener
@Overridepublic 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)); } });}
28
Click Listener
long click listener
map.setOnMapClickListener(new OnMapClickListener() { @Override public void onMapClick(LatLng latLng) { Log.i("map", latLng.latitude + " " + latLng.longitude); }});
29
Click Listener
long click listener
map.setOnMapClickListener(new OnMapClickListener() { @Override public void onMapClick(LatLng latLng) { Log.i("map", latLng.latitude + " " + latLng.longitude); }});
30
Markerpublic 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")); }
}
31
Marker
32
Markerpublic 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
33
Marker
34
Marker
35
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
36
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
37
Shape
38
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 =>