ims.map
Class Map

java.lang.Object
  extended by ims.map.Map
All Implemented Interfaces:
com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks, com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener

public class Map
extends java.lang.Object
implements com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks, com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener

The main class which handles the map-fragment. It sets up the GoogleMap and the main components.

Copyright © 2014 by Daniela Blum

This file is part of GPSChildFinder.
GPSChildFinder is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses.

Author:
Daniela Blum

Constructor Summary
Map(android.content.Context context, android.app.FragmentManager fragmentManager)
          Constructor which sets the context and the fragmentManager of the context to show the map in a fragment of the mainActivity.
 
Method Summary
 void addDeviceLocationMarker(com.google.android.gms.maps.model.MarkerOptions deviceLocationMarkerOptions)
          Adds or rather updates the icon of the marker of the device's location
 void calculateNewBounds(android.view.View.OnClickListener listener)
          Called from ims.map.DeviceButtonListener or MyLocationListener when the new bounds of the screen should be calculated so that both - device and my location - are shown on the map.
 boolean checkLocationSettings()
          Checks which location settings are set on the phone which are needed to show my location (phone's location) on the map.
 void connectMyLocationClient()
          Connects the location client.
 void disconnectMyLocationClient()
          Disconnects the location client and removes all the location updates.
 DeviceLocationButton getDeviceLocationButton()
           
 android.os.Handler getDeviceLocationHandler()
           
 MyLocationButton getMyLocationButton()
           
 void initMap()
          Called in the noCreate() method in the context class (here the MainActivity to initialize the GoogleMap with the aid of the fragmentManager.
 void onConnected(android.os.Bundle arg0)
          Called by Location Services when the request to connect the client finishes successfully.
 void onConnectionFailed(com.google.android.gms.common.ConnectionResult connectionResult)
          Called by Location Services if the attempt to Location Services fails.
 void onDestroy()
          Is called from MainActivity when the Activity gets destroyed.
 void onDisconnected()
          Called by Location Services if the connection to the location client drops because of an error.
 void removeDeviceLocationMarker()
          Removes the marker of the device's location when the location can't be obtained.
 void removeMyLocationMarker()
          Removes the marker of my location (phone's location) when the location can't be obtained.
 void requestMyLocationUpdate()
          Called from MyLocationListener when a update of my location (phones location) is requested from the myLocationClient.
 void selectDeviceLocationButton(java.lang.Boolean selected)
          If deviceLocationButton is selected or unselected the button icon and status gets changed with ImageView.setSelected(boolean) and StatusImageButton.setButtonStatus(ButtonStatus).
 void selectMyLocationButton(java.lang.Boolean selected)
          If myLocationButton is selected or unselected the button icon and status gets changed with ImageView.setSelected(boolean) and StatusImageButton.setButtonStatus(ButtonStatus).
 void setDeviceHandler(android.os.Handler deviceHandler)
           
 void setDeviceLocationButton(DeviceLocationButton deviceLocationButton)
           
 void setMapType(int type)
          Wrapper function.
 void setMyLocationButton(MyLocationButton myLocationButton)
           
 void showDeviceLocation(android.view.View view)
          Method places device's current location as point of interest in the center of the view by moving the map's camera.
 void showMyLocation()
          Method places my/phone's current location as point of interest in the center of the view by moving the map's camera.
 void showToast(int message, int duration)
          Shows a toast on the screen.
 void updateCamera(float bearing)
          Called from MySensorEventListener and moves the camera into a new position after the view direction has changed.
 void updateDirectionArrow()
          Called when the direction arrow should be updated to align in direction of the remote device.
 void updateDistance(boolean remove)
          Is called when the distance should be updated.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Map

public Map(android.content.Context context,
           android.app.FragmentManager fragmentManager)
Constructor which sets the context and the fragmentManager of the context to show the map in a fragment of the mainActivity.

Parameters:
context - context of the map. Normally the MainActivity
fragmentManager - fragment manager of the context to show the map in a frame of the context class
Method Detail

initMap

public void initMap()
Called in the noCreate() method in the context class (here the MainActivity to initialize the GoogleMap with the aid of the fragmentManager.

Sets the GoogleMap type to satellite, initializes the gestureDetector with the supplied listener and calls the methods to setup the main components of the GoogleMap: initMyLocationComponents(), initDeviceLocationComponents(), initDirectionComponents()
Is everything set the map-screen is setup (startScreen() method)


setMapType

public void setMapType(int type)
Wrapper function. Calls the GoogleMap.setMapType(int) method and forwards the map type to set the GoogleMap-type.

Parameters:
type - GoogleMap-type. Can either be GoogleMap.MAP_TYPE_SATELLITE, GoogleMap.MAP_TYPE_TERRAIN or GoogleMap.MAP_TYPE_NORMAL

requestMyLocationUpdate

public void requestMyLocationUpdate()
Called from MyLocationListener when a update of my location (phones location) is requested from the myLocationClient.


selectMyLocationButton

public void selectMyLocationButton(java.lang.Boolean selected)
If myLocationButton is selected or unselected the button icon and status gets changed with ImageView.setSelected(boolean) and StatusImageButton.setButtonStatus(ButtonStatus).
Also the deviceLocationButton methods are called for deselecting and button icon changing that both selected aren't possible.

Parameters:
selected - true if the myLocationButton is pressed/selected by the user, false otherwise

selectDeviceLocationButton

public void selectDeviceLocationButton(java.lang.Boolean selected)
If deviceLocationButton is selected or unselected the button icon and status gets changed with ImageView.setSelected(boolean) and StatusImageButton.setButtonStatus(ButtonStatus).
Also the myLocationButton methods are called for deselecting and button icon changing that both selected aren't possible.

Parameters:
selected - true if the deviceLocationButton is pressed/selected by the user, false otherwise

showMyLocation

public void showMyLocation()
Method places my/phone's current location as point of interest in the center of the view by moving the map's camera. Is also called if the myLocationButton is pressed.
Calling the getMyLocation() method if no location can be demanded.


showDeviceLocation

public void showDeviceLocation(android.view.View view)
Method places device's current location as point of interest in the center of the view by moving the map's camera. Is also called if the deviceLocationButton is pressed.
Method returns when no device is connected or there is no GPS input from it.
Calling the getDeviceLocation() method if no location can be demanded.


addDeviceLocationMarker

public void addDeviceLocationMarker(com.google.android.gms.maps.model.MarkerOptions deviceLocationMarkerOptions)
Adds or rather updates the icon of the marker of the device's location

Parameters:
deviceLocationMarkerOptions - the options to add the marker of the device's location

removeDeviceLocationMarker

public void removeDeviceLocationMarker()
Removes the marker of the device's location when the location can't be obtained.


removeMyLocationMarker

public void removeMyLocationMarker()
Removes the marker of my location (phone's location) when the location can't be obtained.


updateDistance

public void updateDistance(boolean remove)
Is called when the distance should be updated. That happens when whether my location (phone's location) or the location of the GPS-device changes.
Location.distanceBetween(double, double, double, double, float[]) is called to calculate the new distance in meters between the points.

Parameters:
remove - true if distance textView should be removed, false if distance should be updated

updateDirectionArrow

public void updateDirectionArrow()
Called when the direction arrow should be updated to align in direction of the remote device.

First, it stores the location of the device and myLocation locally, to avoid "half"-updates of a location.

Calculation of the rotation degrees:
The angle is calculated with the help of the Math.atan2(double, double) method and the latitudes and longitudes of the two points/locations.
Than the rotation - how much the marker icon should be rotated to point to the device location - is calculated according to the cardinal direction of the device from the location of my phone (north-west, north-east, south-west or south-east of my location). Because the marker icon (the arrow) is standardly pointed to the top of the display, the calculated degrees have to be added or subtracted from an certain amount of degrees to get the correct degrees to rotate.

Finally the myLocationMarkerOptions are updated and addMyLocationMarker(MarkerOptions) is called.


calculateNewBounds

public void calculateNewBounds(android.view.View.OnClickListener listener)
Called from ims.map.DeviceButtonListener or MyLocationListener when the new bounds of the screen should be calculated so that both - device and my location - are shown on the map.
The Method redirects to BoundsCalculator.calculateNewBounds(GoogleMap, LatLng) method.

Parameters:
listener - the listener where the method was called from

updateCamera

public void updateCamera(float bearing)
Called from MySensorEventListener and moves the camera into a new position after the view direction has changed.

Parameters:
bearing - direction of the compass

showToast

public void showToast(int message,
                      int duration)
Shows a toast on the screen.

Parameters:
message - Current relevant message to show on the screen. The resource id of the string resource to use. Can be formatted text.
duration - How long to display the message. Either Toast.LENGTH_SHORT or Toast#LENGTH_LONG

onDestroy

public void onDestroy()
               throws java.lang.NullPointerException
Is called from MainActivity when the Activity gets destroyed.
Removes the callbacks from the deviceLocationHandler. Unregisters the sensorListener from all sensors.

Throws:
java.lang.NullPointerException - when the deviceLocationHandler or updateDeviceLocationRunnable isn't initialized at the moment.

getDeviceLocationHandler

public android.os.Handler getDeviceLocationHandler()
Returns:
current deviceHandler

setDeviceHandler

public void setDeviceHandler(android.os.Handler deviceHandler)
Parameters:
deviceHandler - to set

getMyLocationButton

public MyLocationButton getMyLocationButton()
Returns:
current myLocationButton

setMyLocationButton

public void setMyLocationButton(MyLocationButton myLocationButton)
Parameters:
myLocationButton - to set

getDeviceLocationButton

public DeviceLocationButton getDeviceLocationButton()
Returns:
current deviceLocationButton

setDeviceLocationButton

public void setDeviceLocationButton(DeviceLocationButton deviceLocationButton)
Parameters:
deviceLocationButton - to set

onConnected

public void onConnected(android.os.Bundle arg0)
Called by Location Services when the request to connect the client finishes successfully. At this point, you can request the current location or start periodic updates.

Specified by:
onConnected in interface com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks

onConnectionFailed

public void onConnectionFailed(com.google.android.gms.common.ConnectionResult connectionResult)
Called by Location Services if the attempt to Location Services fails.

Google Play services can resolve some errors it detects. If the error has a resolution, a activity starts that tries to resolve the error, an error dialog is shown otherwise.

Specified by:
onConnectionFailed in interface com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener

onDisconnected

public void onDisconnected()
Called by Location Services if the connection to the location client drops because of an error.

Specified by:
onDisconnected in interface com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks

connectMyLocationClient

public void connectMyLocationClient()
Connects the location client.


disconnectMyLocationClient

public void disconnectMyLocationClient()
Disconnects the location client and removes all the location updates.


checkLocationSettings

public boolean checkLocationSettings()
Checks which location settings are set on the phone which are needed to show my location (phone's location) on the map.

For older versions than android KITKAT it uses a depreciated method.

Returns:
KITKAT and newer: true if the location access is turned on and the location mode is set to Settings.Secure#LOCATION_MODE_HIGH_ACCURACY or Settings.Secure#LOCATION_MODE_BATTERY_SAVING, false otherwise (location access off ( Settings.Secure#LOCATION_MODE_OFF) or device only)
older versions: true if Settings.Secure#LOCATION_PROVIDERS_ALLOWED contains at least one provider, false if its empty.