package com.netfluke.sergey.maptracker /** * Display the map centered on the Green, but keep updating it every time GPS location changes. * The latter must request permission to use GPS. * * TODO: saving location in bundle, handling rotates without fragment destruction * TODO: granularity * */ import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.util.Log import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.OnMapReadyCallback import com.google.android.gms.maps.SupportMapFragment import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.MarkerOptions import android.Manifest import android.content.Context import android.content.pm.PackageManager import android.support.v4.content.ContextCompat import android.widget.Toast import android.location.Location import android.location.LocationListener import android.location.LocationManager class MapsActivity : AppCompatActivity(), OnMapReadyCallback, LocationListener { private lateinit var mMap: GoogleMap private var permCheck : Boolean = false private lateinit var mgr : LocationManager private lateinit var loc : LatLng override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_maps) // Obtain the SupportMapFragment and get notified when the map is ready to be used. val mapFragment = supportFragmentManager .findFragmentById(R.id.map) as SupportMapFragment mapFragment.getMapAsync(this) permCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED if( ! permCheck ){ Toast.makeText(this, "GPS permission FAILED", Toast.LENGTH_LONG).show() } else{ Toast.makeText(this, "GPS permission OK", Toast.LENGTH_LONG).show() mgr = getSystemService(Context.LOCATION_SERVICE) as LocationManager mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, /* milliseconds */ 5f /* meters */ , this); } } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near the Dartmouth Green. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ override fun onMapReady(googleMap: GoogleMap) { mMap = googleMap // Add a marker and move the camera to it val x : Double = resources.getString(R.string.theGreen_x).toDouble() val y : Double = resources.getString(R.string.theGreen_y).toDouble() val hanover = LatLng( x, y ) var l : Location? = null // remains null if Location is disabled in the phone try { l = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER) } catch( e: SecurityException ){ Log.d("PERM", "Security Exception getting last known location. Using Hanover.") } loc = if (l != null) LatLng(l.latitude, l.longitude) else hanover Log.d("Coords", loc.latitude.toString() + " " + loc.longitude.toString() ) mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); mMap.addMarker(MarkerOptions().position(hanover).title("Marker in Hanover")) mMap.moveCamera(CameraUpdateFactory.newLatLng(hanover)) mMap.moveCamera(CameraUpdateFactory.zoomTo(17f)) // buildings-level mMap.setOnMapClickListener { p0: LatLng? -> Log.d( "Map", p0.toString()) if( p0 != null ) { mMap.addMarker(MarkerOptions().position(p0).title(p0.toString())) } } } override fun onLocationChanged(location : Location){ Log.d("LOCATION", "CHANGED: " + location.latitude + " " + location.longitude) Toast.makeText(this, "LOC: " + location.latitude + " " + location.longitude, Toast.LENGTH_LONG).show() val newPoint = LatLng( location.latitude, location.longitude ) mMap.moveCamera(CameraUpdateFactory.newLatLng(newPoint)) mMap.moveCamera(CameraUpdateFactory.zoomTo(17f)) } override fun onProviderDisabled(s: String) { // required for interface, not used } override fun onProviderEnabled(s: String) { // required for interface, not used } override fun onStatusChanged(s: String, i: Int, bundle: Bundle) { // required for interface, not used } }