Documente Academic
Documente Profesional
Documente Cultură
socialhelper
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Build
import android.provider.Settings
import android.support.v4.app.ActivityCompat
import android.support.v4.app.FragmentActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.widget.Toast
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.BitmapDescriptorFactory
import com.google.android.gms.maps.model.CameraPosition
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.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import com.yusufemre.socialhelper.model.LocationData
import java.text.SimpleDateFormat
import java.util.ArrayList
import java.util.Date
/**
* 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 Sydney, Australia.
* 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
val cameraPosition =
CameraPosition.Builder().zoom(15f).target(recife).build()
mMap!!.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}
if (currentLocationMaker != null) {
currentLocationMaker!!.remove()
}
//Add marker
currentLocationLatLong = LatLng(location.latitude, location.longitude)
val markerOptions = MarkerOptions()
markerOptions.position(currentLocationLatLong!!)
markerOptions.title("Localização atual")
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HU
E_BLUE))
currentLocationMaker = mMap!!.addMarker(markerOptions)
mDatabase!!.child("location").child(Date().time.toString()).setValue(locationData)
return result
}
fun showSettingsAlert() {
val alertDialog = AlertDialog.Builder(this)
alertDialog.setTitle("GPS desativado!")
alertDialog.setMessage("Ativar GPS?")
alertDialog.setPositiveButton("Sim") { dialog, which ->
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
}
alertDialog.show()
}
permissions.add(android.Manifest.permission.ACCESS_FINE_LOCATION)
permissions.add(android.Manifest.permission.ACCESS_COARSE_LOCATION)
permissionsToRequest = findUnAskedPermissions(permissions) as
ArrayList<String>
//Check if GPS and Network are on, if not asks the user to turn on
if (!isGPS && !isNetwork) {
showSettingsAlert()
} else {
// check permissions
} else if (isNetwork) {
// from Network Provider
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this)
}
} else {
Toast.makeText(this, "Não é possível obter a localização",
Toast.LENGTH_SHORT).show()
}
}
mDatabase!!.child("location").addListenerForSingleValueEvent(
object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
//Get map of users in datasnapshot
if (dataSnapshot.value != null)
getAllLocations((dataSnapshot.value as Map<String,
Any>?)!!)
}
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HU
E_GREEN))
mMap!!.addMarker(markerOptions)
}
}
}