Skip to content

Instantly share code, notes, and snippets.

@fida1989
Last active March 31, 2019 06:56
Show Gist options
  • Save fida1989/7cbefa4f63cb1c04c7e0c61141bc42f2 to your computer and use it in GitHub Desktop.
Save fida1989/7cbefa4f63cb1c04c7e0c61141bc42f2 to your computer and use it in GitHub Desktop.
MapsActivity
import android.Manifest
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.location.Location
import android.os.Bundle
import android.os.Handler
import android.os.Parcelable
import android.os.SystemClock
import android.provider.Settings
import android.support.v4.app.ActivityCompat
import android.support.v4.content.LocalBroadcastManager
import android.view.MenuItem
import android.view.animation.AccelerateDecelerateInterpolator
import com.androidnetworking.AndroidNetworking
import com.androidnetworking.common.Priority
import com.androidnetworking.error.ANError
import com.androidnetworking.interfaces.StringRequestListener
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.Marker
import com.google.android.gms.maps.model.MarkerOptions
import kotlinx.android.synthetic.main.activity_maps.*
import org.json.JSONArray
import org.json.JSONException
import java.util.*
class MapsActivity : AnimatedActivity(), OnMapReadyCallback {
private var mMap: GoogleMap? = null
private var mapFragment: SupportMapFragment? = null
private var marker: Marker? = null
private val locationList = ArrayList<PassengerLocation>()
private val socket: Socket? = null
/**
* handle new location
*/
private val mLocationUpdated = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
try {
val location = intent.getParcelableExtra<Parcelable>(Constants.LBM_EVENT_LOCATION_UPDATE) as Location
println("Lat: " + location.latitude
+ " Lon: " + location.longitude)
if (mMap != null) {
if (marker != null) {
moveMarker(marker, location)
} else {
setMarker(location.latitude, location.longitude)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
//val myToolbar = findViewById<View>(R.id.my_toolbar) as Toolbar
setSupportActionBar(my_toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment?
if (mapFragment != null) {
mapFragment!!.getMapAsync(this)
}
mapFragment!!.getMapAsync(this)
}
/*
Setting vehicle marker on the map
*/
private fun setMarker(lat: Double, lon: Double) {
var move = true
if (marker != null) {
marker!!.remove()
move = false
}
val me = LatLng(lat, lon)
marker = mMap!!.addMarker(MarkerOptions().position(me).title("Marker"))
if (move) {
mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(me, 15f))
}
}
private fun setPointMarker(point: String?, lat: Double, lon: Double) {
println("lat$lat")
val me1 = LatLng(lat, lon)
if (mMap != null) {
mMap!!.addMarker(MarkerOptions().position(me1).title(point))
}
}
/**
* 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.
*/
@SuppressLint("MissingPermission")
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mMap!!.uiSettings.isMapToolbarEnabled = false
mMap!!.uiSettings.isZoomControlsEnabled = true
mMap!!.uiSettings.isMyLocationButtonEnabled = true
mMap!!.uiSettings.isCompassEnabled = true
if (FusedLocationSingleton.getInstance().lastLocation != null) {
println("Yes Last Location!")
val lat = FusedLocationSingleton.getInstance().lastLocation!!.latitude
val lon = FusedLocationSingleton.getInstance().lastLocation!!.longitude
setMarker(lat, lon)
} else {
println("No Last Location!")
}
}
fun displayGPSSettingsDialog() {
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
}
override fun onResume() {
super.onResume()
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// start location updates
FusedLocationSingleton.getInstance().startLocationUpdates()
// register observer for location updates
LocalBroadcastManager.getInstance(this@MapsActivity).registerReceiver(mLocationUpdated, IntentFilter(Constants.INTENT_FILTER_LOCATION_UPDATE))
}
}
/***********************************************************************************************
* local broadcast receiver
*//*
*/
override fun onPause() {
super.onPause()
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// stop location updates
FusedLocationSingleton.getInstance().stopLocationUpdates()
// unregister observer
LocalBroadcastManager.getInstance(this@MapsActivity).unregisterReceiver(mLocationUpdated)
}
}
fun moveMarker(myMarker: Marker?, finalPosition: Location) {
val startPosition = myMarker!!.position
println("first$startPosition")
println("second $finalPosition")
val handler = Handler()
val start = SystemClock.uptimeMillis()
val interpolator = AccelerateDecelerateInterpolator()
val durationInMs = 3000f
val hideMarker = false
handler.post(object : Runnable {
internal var elapsed: Long = 0
internal var t: Float = 0.toFloat()
internal var v: Float = 0.toFloat()
override fun run() {
// Calculate progress using interpolator
elapsed = SystemClock.uptimeMillis() - start
t = elapsed / durationInMs
v = interpolator.getInterpolation(t)
val currentPosition = LatLng(
startPosition.latitude * (1 - t) + finalPosition.latitude * t,
startPosition.longitude * (1 - t) + finalPosition.longitude * t)
marker!!.position = currentPosition
// Repeat till progress is complete else
if (t < 1) {
// Post again 16ms later.
handler.postDelayed(this, 16)
} else {
marker!!.isVisible = !hideMarker
}
mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 15f))
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment