- Ref. CN5E, NT@UW, WUSTL
CS5530
CS5530 Mobile/Wireless Systems Using Google Map in Android
Yanyan Zhuang
Department of Computer Science http://www.cs.uccs.edu/~yzhuang
- UC. Colorado Springs
CS5530 Mobile/Wireless Systems Using Google Map in Android Yanyan - - PowerPoint PPT Presentation
CS5530 Mobile/Wireless Systems Using Google Map in Android Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang UC. Colorado Springs CS5530 Ref. CN5E, NT@UW, WUSTL Setup Install the Google Play services SDK o
CS5530
2 CS5530
3 CS5530
4 CS5530
Android API, and it supports an unlimited number of users
5 CS5530
6 CS5530
7 CS5530
<fragment name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/>
8 CS5530
} sort of like a "sub activity" that you can reuse in different activities
9 CS5530
10 CS5530
implements OnMapReadyCallback { // OnCreate() method here: as described on last slide @Override public void onMapReady(GoogleMap googleMap) { // Add a marker in Sydney, Australia, and move map's camera LatLng sydney = new LatLng(-33.852, 151.211); googleMap.addMarker(new MarkerOptions().position(sydney) .title("Marker in Sydney")); googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } }
11 CS5530
12 CS5530
1.
2.
3.
4.
13 CS5530
... // Sets the map type to be "hybrid" map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
14 CS5530
} googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in
Sydney"));
15 CS5530
16 CS5530
17 CS5530
} @Override
public void onMapReady(GoogleMap googleMap) { …... mMap.setOnMarkerClickListener(this); }
18 CS5530
} @Override
public boolean onMarkerClick(final Marker marker) { if (marker.equals(myMarker)) { Toast.makeText(this, marker.getTitle() + " has been clicked!", Toast.LENGTH_LONG).show(); } return false; }
19 CS5530
20 CS5530
21 CS5530
} toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); } If you want to nudge the position to the right, increase the value of the second
22 CS5530
} private GoogleApiClient mGoogleApiClient;
23 CS5530
24 CS5530
25 CS5530
public void onConnected(Bundle connectionHint) { ... } /* Handles failure to connect to the Google Play services client. */ @Override public void onConnectionFailed(@NonNull ConnectionResult result) { Log.d(TAG, "Play services connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); } /* Handles suspension of the connection to the Google Play services client. */ @Override public void onConnectionSuspended(int cause) { Log.d(TAG, "Play services connection suspended"); }
26 CS5530
27 CS5530
28 CS5530
29 CS5530
30 CS5530
@Override public void onResult(@NonNull PlaceLikelihoodBuffer likelyPlaces) { mLikelyPlaceNames = new String[mMaxEntries]; … for (PlaceLikelihood placeLikelihood : likelyPlaces) { // Build a list of likely places to show the user. Max 5. mLikelyPlaceNames[i] = (String) placeLikelihood.getPlace().getName(); … } // Release the place likelihood buffer, to avoid memory leaks. likelyPlaces.release(); } });
31 CS5530
} Set a list of items to be displayed in the dialog as the content } Code will be notified of the selected item via the supplied listener } Implement the listener
32 CS5530
new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // The "which" argument contains the position of the selected item. LatLng markerLatLng = mLikelyPlaceLatLngs[which]; String markerSnippet = mLikelyPlaceAddresses[which]; ... // Add a marker for the selected place, with an info window w/ information about that place mMap.addMarker(new MarkerOptions() .title(mLikelyPlaceNames[which]) .position(markerLatLng) .snippet(markerSnippet)); // Position the map's camera at the location of the marker. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(markerLatLng, DEFAULT_ZOOM)); } };