Code example for GoogleMap

Methods: aW, setOnCameraChangeListener

0
/* 
 * Copyright 2013 Taeho Kim (jyte82@gmail.com) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */ 
package com.androidhuman.google.maps.v2.support; 
 
import java.util.List; 
 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.location.Location; 
import android.preference.PreferenceManager; 
 
import com.androidhuman.google.maps.v2.support.model.SupportMarker; 
import com.google.android.gms.maps.CameraUpdate; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener; 
import com.google.android.gms.maps.LocationSource; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.Projection; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.UiSettings; 
import com.google.android.gms.maps.model.CameraPosition; 
import com.google.android.gms.maps.model.GroundOverlayOptions; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.android.gms.maps.model.PolygonOptions; 
import com.google.android.gms.maps.model.Polyline; 
import com.google.android.gms.maps.model.PolylineOptions; 
import com.google.android.gms.maps.model.TileOverlay; 
import com.google.android.gms.maps.model.TileOverlayOptions; 
 
/** 
 * This is the 'wrapper' of the main class of the Google Maps Android API and is the entry point  
 * for all methods related to the map. 
 * @author Taeho Kim 
 * 
 */ 
public class SupportGoogleMap implements OnCameraChangeListener{
	private static final String KEY_LAST_LATITUDE = "com_androidhuman_google_maps_v2_support_last_latitude";
	private static final String KEY_LAST_LONGITUDE = "com_androidhuman_google_maps_v2_support_last_longitude";
	private static final String KEY_LAST_ZOOM = "com_androidhuman_google_maps_v2_support_last_zoom";
	 
	private static final int INVALID_VALUE = -1000;
	 
	private boolean mRememberLastCamPosition = true;
	private boolean mIsInitialPositionRequested = false;
	private boolean mIsInitialZoomLevelRequested = false;
	 
	private SharedPreferences mSharedPreferences;
	private SharedPreferences.Editor mSharedPreferenceEditor;
	 
	private GoogleMap mGoogleMap;
	private MarkerManager mMarkerManager;
	private PolylineManager mPolylineManager;
	 
	private OnCameraChangeListener mCameraChangeListener;
	 
	private SupportGoogleMap(Context context){
		mMarkerManager = new MarkerManager();
		mPolylineManager = new PolylineManager();
		 
		mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		mSharedPreferenceEditor = mSharedPreferences.edit();
	} 
	 
	private SupportGoogleMap(Context context, GoogleMap map) throws NoPlayServicesFoundException{
		this(context);
		setGoogleMap(map);
	} 
	 
	/** 
	 * Adds an image to this map. 
	 * @param options A ground-overlay options object that defines how to render the overlay.  
	 * Options must have an image (AnchoredBitmap) and position specified. 
	 * @throws IllegalArgumentException if either the image or the position is unspecified in the options. 
	 */ 
	public void addGroundOverlay(GroundOverlayOptions options){
		mGoogleMap.addGroundOverlay(options);
	} 
 
	/** 
	 * Adds a marker to this map.<p> 
	 * The marker's icon is rendered on the map at the location Marker.position.  
	 * Clicking the marker centers the camera on the marker.  
	 * If Marker.title is defined, the map shows an info box with the marker's title and snippet.  
	 * If the marker is draggable, long-clicking and then dragging the marker moves it. 
	 * @param options A marker options object that defines how to render the marker. 
	 * @return The {@link SupportMarker} object that was added 
	 */ 
	public SupportMarker addMarker(MarkerOptions options){
		return mMarkerManager.add(options);
	} 
	 
	/** 
	 * Adds a marker to map with auto-generated marker id. 
	 * @param title A title of the marker 
	 * @param position Marker's position 
	 * @return The {@link SupportMarker} object that was added 
	 */ 
	public SupportMarker addMarker(String title, LatLng position){
		return mMarkerManager.add(title, position);
	} 
	 
	/** 
	 * Adds a marker to map with auto-generated marker id. 
	 * @param title A title of the marker 
	 * @param snippet A snippet of the marker 
	 * @param position Marker's position 
	 * @return The {@link SupportMarker} object that was added 
	 */ 
	public SupportMarker addMarker(String title, String snippet, LatLng position){
		return mMarkerManager.add(title, snippet, position);
	} 
	 
	/** 
	 * Adds a marker to map with given marker id. 
	 * @param id Marker's id 
	 * @param options A marker options object that defines how to render the marker. 
	 * @return The {@link SupportMarker} object that was added 
	 */ 
	public SupportMarker addMarker(long id, MarkerOptions options){
		return mMarkerManager.add(id, options);
	} 
	 
	public void addPolygon(PolygonOptions options){
		mGoogleMap.addPolygon(options);
	} 
	 
	/** 
	 * Adds a polyline to this map. 
	 * @param options A polyline options object that defines how to render the Polyline. 
	 * @return The id of the polyline that was added to map. 
	 */ 
	public long addPolyline(PolylineOptions options){
		return mPolylineManager.add(options);
	} 
	 
	/** 
	 * Adds a polyline to this map. 
	 * @param points Array of positions composing polyline 
	 * @return The id of the polyline that was added to map. 
	 */ 
	public long addPolyline(LatLng... points){
		return mPolylineManager.add(points);
	} 
	 
	/** 
	 * Adds a polyline to this map. 
	 * @param points Array of positions composing polyline 
	 * @return The id of the polyline that was added to map. 
	 */ 
	public long addPolyline(List<LatLng> points){
		return mPolylineManager.add(points);
	} 
	 
	/** 
	 * Adds a polyline to this map. 
	 * @param color The color of the polyline 
	 * @param points Array of positions composing polyline 
	 * @return The id of the polyline that was added to map. 
	 */ 
	public long addPolyline(int color, LatLng... points){
		return mPolylineManager.add(color, points);
	} 
	 
	/** 
	 * Adds a polyline to this map. 
	 * @param color The color of the polyline 
	 * @param points Array of positions composing polyline 
	 * @return The id of the polyline that was added to map. 
	 */ 
	public long addPolyline(int color, List<LatLng> points){
		return mPolylineManager.add(color, points);
	} 
	 
	/** 
	 * Adds a polyline to this map with given polyline id. 
	 * @param id Polyline's id 
	 * @param options A polyline options object that defines how to render the Polyline. 
	 */ 
	public void addPolyline(long id, PolylineOptions options){
		mPolylineManager.add(id, options);
	} 
	 
	/** 
	 * Appends a point to an existing polyline. 
	 * @param id Polyline's id 
	 * @param point a Point to append with 
	 */ 
	public void appendPolyline(long id, LatLng point, boolean shouldAnimateToLastPoint){
		mPolylineManager.append(id, point);
		if(shouldAnimateToLastPoint){
			animateTo(point);
		} 
	} 
	 
	/** 
	 * Adds a tile overlay to this map. See TileOverlay for more information.<p> 
	 * Note that unlike other overlays, if the map is recreated, tile overlays are not  
	 * automatically restored and must be re-added manually. 
	 * @param options A tile-overlay options object that defines how to render the overlay.  
	 * Options must have a TileProvider specified, otherwise an IllegalArgumentException  
	 * will be thrown. 
	 * @returns The TileOverlay that was added to the map. 
	 */ 
	public TileOverlay addTileOverlay(TileOverlayOptions options){
		return mGoogleMap.addTileOverlay(options);
	} 
	 
	/** 
	 * Moves the map according to the update with an animation over a specified duration,  
	 * and calls an optional callback on completion. See CameraUpdateFactory for a set of updates. 
	 * If getCameraPosition() is called during the animation, it will return the current  
	 * location of the camera in flight. 
	 * @param update The change that should be applied to the camera. 
	 * @param durationMs The duration of the animation in milliseconds.  
	 * This must be strictly positive, otherwise an IllegalArgumentException will be thrown. 
	 * @param callback An optional callback to be notified from the main thread  
	 * when the animation stops. If the animation stops due to its natural completion,  
	 * the callback will be notified with onFinish().  
	 * If the animation stops due to interruption by a later camera movement or a user gesture,  
	 * onCancel() will be called. The callback should not attempt to move or animate the camera  
	 * in its cancellation method. 
	 */ 
	public void animateCamera(CameraUpdate update, int durationMs, GoogleMap.CancelableCallback callback){
		mGoogleMap.animateCamera(update, durationMs, callback);
	} 
	 
	/** 
	 * Animates the movement of the camera from the current position to the position defined  
	 * in the update and calls an optional callback on completion.  
	 * See CameraUpdateFactory for a set of updates.<p> 
	 * During the animation, a call to getCameraPosition() returns an intermediate location  
	 * of the camera. 
	 * @param update The change that should be applied to the camera. 
	 * @param callback The callback to invoke from the main thread when the animation stops.  
	 * If the animation completes normally, onFinish() is called; otherwise, onCancel() is called.  
	 * Do not update or animate the camera from within onCancel(). 
	 */ 
	public void animateCamera(CameraUpdate update, GoogleMap.CancelableCallback callback){
		mGoogleMap.animateCamera(update, callback);
	} 
	 
	/** 
	 * Animates the movement of the camera from the current position to the position defined  
	 * in the update. During the animation, a call to getCameraPosition() returns an intermediate  
	 * location of the camera. <p>See CameraUpdateFactory for a set of updates. 
	 * @param update The change that should be applied to the camera. 
	 */ 
	public void animateCamera(CameraUpdate update){
		mGoogleMap.animateCamera(update);
	} 
	 
	/** 
	 * Animates the movement of the camera from the current position to the position defined  
	 * in the update. During the animation, a call to getCameraPosition() returns an intermediate  
	 * location of the camera. <p>See CameraUpdateFactory for a set of updates. 
	 * @param position 
	 */ 
	public void animateTo(LatLng position){
		mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(position));
	} 
	 
	/** 
	 * Animates the movement of the camera from the current position to the position defined  
	 * in the update. During the animation, a call to getCameraPosition() returns an intermediate  
	 * location of the camera. <p>See CameraUpdateFactory for a set of updates. 
	 * @param marker 
	 */ 
	public void animateTo(SupportMarker marker){
		mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getMarker().getPosition()));
	} 
	 
	/** 
	 * Removes all markers, overlays, and polylines from the map. 
	 */ 
	public void clear(){ 
		mGoogleMap.clear();
		mMarkerManager.clear(false);
	} 
	 
	/** 
	 * Gets the current position of the camera.<p> 
	 * The CameraPosition returned is a snapshot of the current position,  
	 * and will not automatically update when the camera moves. 
	 * @return The current position of the Camera. 
	 */ 
	public CameraPosition getCameraPosition(){
		return mGoogleMap.getCameraPosition();
	} 
	 
	/** 
	 * Gets the type of map that's currently displayed. 
	 * @return 
	 */ 
	public int getMapType(){ 
		return mGoogleMap.getMapType();
	} 
	 
	public Marker getMarker(long markerId){
		return mMarkerManager.getMarker(markerId);
	} 
	 
	/** 
	 * Returns the maximum zoom level for the current camera position.  
	 * This takes into account what map type is currently being used, e.g., satellite or  
	 * terrain may have a lower max zoom level than the base map tiles. 
	 * @return The maximum zoom level available at the current camera position. 
	 */ 
	public float getMaxZoomLevel(){ 
		return mGoogleMap.getMaxZoomLevel();
	} 
	 
	/** 
	 * Returns the minimum zoom level. This is the same for every location  
	 * (unlike the maximum zoom level) but may vary between devices and map sizes. 
	 * @return The minimum zoom level available. 
	 */ 
	public float getMinZoomLevel(){ 
		return mGoogleMap.getMinZoomLevel();
	} 
	 
	/** 
	 * Returns the currently displayed user location, or null if there is no location data available. 
	 * @return The currently displayed user location. 
	 */ 
	public Location getMyLocation(){
		return mGoogleMap.getMyLocation();
	} 
	 
	public Polyline getPolyline(long id){
		return mPolylineManager.getPolyline(id);
	} 
	 
	public Projection getProjection(){
		return mGoogleMap.getProjection();
	} 
	 
	public UiSettings getUiSettings(){
		return mGoogleMap.getUiSettings();
	} 
	 
	public boolean isIndoorEnabled(){ 
		return mGoogleMap.isIndoorEnabled();
	} 
	 
	public boolean isMyLocationEnabled(){ 
		return mGoogleMap.isMyLocationEnabled();
	} 
	 
	public boolean isTrafficEnabled(){ 
		return mGoogleMap.isTrafficEnabled();
	} 
	 
	public void moveCamera(CameraUpdate update){
		mGoogleMap.moveCamera(update);
	} 
	 
	public void moveTo(LatLng position){
		mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(position));
	} 
	 
	public void moveTo(SupportMarker marker){
		mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(marker.getMarker().getPosition()));
	} 
	 
	public static SupportGoogleMap newInstance(Context context, MapFragment mapFragment) throws NoPlayServicesFoundException{
		SupportGoogleMap map = new SupportGoogleMap(context, mapFragment.getMap());
		return map;
	} 
	 
	public static SupportGoogleMap newInstance(Context context, SupportMapFragment mapFragment) throws NoPlayServicesFoundException{
		SupportGoogleMap map = new SupportGoogleMap(context, mapFragment.getMap());
		return map;
	} 
	 
	public static SupportGoogleMap newInstance(Context context, GoogleMap map) throws NoPlayServicesFoundException{
		SupportGoogleMap mapInstance = new SupportGoogleMap(context, map);
		return mapInstance;
	} 
	 
	/** 
	 * Set whether SupportGoogleMap should remember and load last CameraPosition 
	 * of the GoogleMap, which includes latitude, longitude, zoom level. 
	 * Enabled by default. 
	 * @param enabled true to use this feature, false to disable. 
	 */ 
	public void rememberAndLoadLastCameraPosition(boolean enabled){
		this.mRememberLastCamPosition = enabled;
	} 
	 
	public void setIndoorEnabled(boolean enabled){
		mGoogleMap.setIndoorEnabled(enabled);
	} 
	 
	public void setInfoWindowAdapter(GoogleMap.InfoWindowAdapter adapter){
		mGoogleMap.setInfoWindowAdapter(adapter);
	} 
	 
	public void setInitialCameraPosition(LatLng position){
		mIsInitialPositionRequested = true;
		mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(position));
	} 
	 
	public void setInitialCameraPosition(LatLng posotion, float zoomLevel){
		mIsInitialPositionRequested = true;
		mIsInitialZoomLevelRequested = true;
		mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(posotion, zoomLevel));
	} 
	 
	public void setLocationSource(LocationSource source){
		mGoogleMap.setLocationSource(source);
	} 
	 
	public void setMapType(int type){
		mGoogleMap.setMapType(type);
	} 
	 
	public void setMyLocationEnabled(boolean enabled){
		mGoogleMap.setMyLocationEnabled(enabled);
	} 
 
	public void setGoogleMap(GoogleMap map) throws NoPlayServicesFoundException {
		if(map==null){
			throw new NoPlayServicesFoundException(); 
		} 
		mGoogleMap = map;
		mGoogleMap.setOnCameraChangeListener(this);
		 
		mMarkerManager.setGoogleMap(map);
		mPolylineManager.setGoogleMap(map);
		 
		if(mRememberLastCamPosition){
			float lastLatitude = mSharedPreferences.getFloat(KEY_LAST_LATITUDE, INVALID_VALUE);
			float lastLongitude = mSharedPreferences.getFloat(KEY_LAST_LONGITUDE, INVALID_VALUE);
			float lastZoom = mSharedPreferences.getFloat(KEY_LAST_ZOOM, INVALID_VALUE);