Code example for GoogleMap

Methods: addCircle, addMarker, addPolygon, addPolyline, getUiSettings, setMapType

0
	@Override 
	public void onViewCreated(View view, Bundle savedInstanceState) {
 
		map = getMap();
 
		UiSettings uiSettings = map.getUiSettings();
		uiSettings.setCompassEnabled(true);
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		uiSettings.setScrollGesturesEnabled(false);
		uiSettings.setRotateGesturesEnabled(false);
		uiSettings.setZoomGesturesEnabled(true);
		uiSettings.setZoomControlsEnabled(true);
		 
		Location lastLocation = getBestLastKnownLocation();
		LatLng lastLatLng;
		 
		if(lastLocation == null) {
			lastLatLng = UserModel.getLocation(getSherlockActivity());
		} else { 
			lastLatLng = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
			 
			if(UserModel.getLocation(getSherlockActivity()) == null) 
			{ 
				UserModel.setLocation(getSherlockActivity(), lastLocation);
			}	 
		} 
		 
		if(lastLatLng != null) showMyLocation(lastLatLng); 
		 
		if(MatchModel.inActiveMatch(getSherlockActivity())) { 
			showGameBoundary(); 
			showRangeCircles(lastLatLng);
			showDirectionToTarget(tBearing);
			showTargetLocation(PlayerModel.getTargetLocation(getSherlockActivity())); 
		} 
		 
		moveMapPositionTo(lastLatLng, DEFAULT_ZOOM, DEFAULT_TILT, true, 2000);
         
		super.onViewCreated(view, savedInstanceState);
	} 
 
	@Override 
	public void onResume() { 
		super.onResume(); 
		 
		if(MODE == MODE_BEARING) {
			registerForSensorUpdates(); 
		} 
		else if (MODE == MODE_NORTH) {
			stopSensorUpdates(); 
		} 
	} 
 
	@Override 
	public void onPause() { 
		super.onPause(); 
		stopSensorUpdates(); 
		 
	} 
	 
	public int getCompassMode() { 
		return this.MODE;
	} 
	 
	public void toggleCompassMode() { 
		if(getCompassMode() == MODE_BEARING) {
			MODE = MODE_NORTH;
			stopSensorUpdates(); 
			this.moveMapPositionTo(UserModel.getLocation(getSherlockActivity()), true, 800);
			map.getUiSettings().setZoomControlsEnabled(true);
		} else  { 
			MODE = MODE_BEARING;
			map.getUiSettings().setZoomControlsEnabled(false);
			if(!animating)
				registerForSensorUpdates(); 
		} 
	} 
 
	public void setBearingProvider(BearingProvider bp) {
		bearingProvider = bp;
	} 
	 
	private void stopSensorUpdates() { 
		if(bearingProvider != null) bearingProvider.unregisterForBearingUpdates(this);
	} 
 
	private void registerForSensorUpdates() { 
		if(bearingProvider != null) bearingProvider.registerForBearingUpdates(this);
	} 
	public void onLocationChanged(LatLng location) {
		showMyLocation(location); 
		 
		if(MatchModel.inActiveMatch(getActivity())) { 
			showRangeCircles(location);
			showDirectionToTarget(tBearing);
		} else { 
			hideRangeCircles(); 
			hideTargetLocation(); 
			hideDirectionToTarget(); 
		} 
		 
		moveMapPositionTo(location);
	} 
	 
	public void moveMapPositionTo(LatLng location) {
		moveMapPositionTo(location, false, null);
	} 
	 
	public void moveMapPositionTo(LatLng location, boolean animate, Integer duration) {
		CameraPosition cam = map.getCameraPosition();
		moveMapPositionTo(location, cam.zoom, cam.tilt, animate, duration) ;
	} 
	 
	public void moveMapPositionTo(LatLng location, Float zoom, Float tilt, boolean animate, Integer duration) {
 
		if(!animating && location != null) {
			Log.d(TAG, "move map to [position=" + location.toString() + 
					", bearing=" + bearing + 
					", animate="+ animate + "]");
 
			CameraPosition cameraPosition = new CameraPosition.Builder()
				.target(location)
				.zoom(zoom != null ? zoom : DEFAULT_ZOOM)              
				.bearing(MODE == MODE_NORTH  ? 0 : bearing)
				.tilt(tilt != null ? tilt : DEFAULT_TILT)                
				.build(); 	
			 
			CameraUpdate camUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
			 
			if(animate) {
				stopSensorUpdates(); 
				animating = true;
				map.animateCamera(camUpdate, duration != null ? duration : 1000, 
					new CancelableCallback(){
						@Override 
						public void onCancel() { 
							animating = false;
							if(MODE == MODE_BEARING)
								registerForSensorUpdates(); 
						} 
						@Override 
						public void onFinish() { 
							animating = false;
							if(MODE == MODE_BEARING)
								registerForSensorUpdates(); 
						} 
				}); 
			} else { 
				//map.stopAnimation(); 
				map.moveCamera(camUpdate);
			} 
		} 
	} 
 
	public void showMyLocation(LatLng location) {
		if(myLocationMarker == null) {	
			myLocationMarker = map.addMarker(new MarkerOptions()
				.position(location)
				.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
		} else { 
			myLocationMarker.setPosition(location);
		} 
	} 
	 
	public void showTargetLocation(LatLng tLatLng) {
		if(tLatLng != null) {
			if(targetLocationMarker == null) {
				targetLocationMarker = getMap().addMarker(
			    		new MarkerOptions()
			    		.position(tLatLng)
			    		.title("target")
			    		.snippet(PlayerModel.getTargetLife(getActivity()).toString())
			    		.icon(BitmapDescriptorFactory 
			    				.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
				targetLocationMarker.showInfoWindow();
			} 
			else 
			{ 
				targetLocationMarker.setPosition(tLatLng);
			} 
			 
			targetLocationMarker.setVisible(true);
		} 
	}	 
 
	public void showGameBoundary() { 
		if(boundsPolygon == null) {
			boundsPolygon = map.addPolygon(new PolygonOptions()
				.zIndex(0)
				//TODO: show actual match bounds polygon 
				.add(new LatLng(40.3, -111.6), new LatLng(40.4, -111.6), new LatLng(40.4, -111.7), new LatLng(40.3, -111.7))
			    .strokeColor(BLUE)
			    .fillColor(Color.TRANSPARENT));
		} 
	} 
 
	public void showRangeCircles(LatLng location) {
		Double aRange = MatchModel.getAttackRange(getActivity());
		 
		//if in match with attack range, draw/update the circle position 
		if(aRange != null) {
			if(aRangeCircle == null) {
				aRangeCircle = map.addCircle(new CircleOptions()
				     .center(location)
				     .radius(aRange*METERS_PER_MILE)
				     .strokeColor(RED)
				     .strokeWidth(5)
				     .fillColor(Color.TRANSPARENT));
			} else { 
				aRangeCircle.setCenter(location);
			} 
		} else if (aRangeCircle != null) {
			//if not in a match and showing an attack range circle, clear it 
			aRangeCircle.remove();
		} 
		 
		Double hRange = MatchModel.getHuntRange(getActivity());
		 
		if(hRange != null) {
			if(hRangeCircle == null) {
				hRangeCircle = map.addCircle(new CircleOptions()
				     .center(location)
				     .radius(hRange*METERS_PER_MILE)
				     .strokeColor(BLUE)
				     .strokeWidth(5)
				     .fillColor(Color.TRANSPARENT));
			} else { 
				hRangeCircle.setCenter(location);
			} 
		} else if (hRangeCircle != null) {
			hRangeCircle.remove();
		} 
		 
	} 
 
	private void showDirectionToTarget(Float tBearing) {
		 
		Double aRange = MatchModel.getAttackRange(this.getActivity());
		 
		if(tBearing != null && myLocationMarker != null && aRange != null) {
		 
			LatLng myLocation = this.myLocationMarker.getPosition();
			 
			tBearing = (float) Math.toRadians(tBearing);
			 
			double dist = aRange/RADIUS_OF_EARTH;
			double lat1 = Math.toRadians(myLocation.latitude);
			double lon1 = Math.toRadians(myLocation.longitude);
	 
			double lat2 = Math.asin( Math.sin(lat1)*Math.cos(dist) + Math.cos(lat1)*Math.sin(dist)*Math.cos(tBearing) );
			double a = Math.atan2(Math.sin(tBearing)*Math.sin(dist)*Math.cos(lat1), Math.cos(dist)-Math.sin(lat1)*Math.sin(lat2));
			double lon2 = lon1 + a;
			//Is this necessary? 
			//lon2 = (lon2+ 3.0*Math.PI) % (2.0*Math.PI) - Math.PI; 
			 
			if(tBearingLine != null) {
				tBearingLine.remove();
			} 
			 
			tBearingLine = map.addPolyline(new PolylineOptions()
					.add(myLocation, new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2)))
					.color(RED)
					.width(5));
		} 
		else if(tBearingLine != null) {