Created
November 12, 2020 07:15
-
-
Save NurseyitTursunkulov/6a7f497c56cb051b9c6ffb8707896c35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainMapsFragment : Fragment(), CameraListener { | |
var lonLatOfBishkek: LonLat = LonLat(42.831698, 74.621554) | |
private val MAPKIT_API_KEY = "5e4b8c11-3756-4222-ae2d-062f9e631c4f" | |
private var mapObjects: MapObjectCollection? = null | |
private val DRAGGABLE_PLACEMARK_CENTER = Point(42.831698, 74.621554) | |
private lateinit var searchManager: SearchManager | |
val viewModel: MainViewModel by sharedViewModel() | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
MapKitFactory.setApiKey(MAPKIT_API_KEY) | |
MapKitFactory.initialize(requireContext()) | |
SearchFactory.initialize(requireContext()) | |
searchManager = SearchFactory.getInstance().createSearchManager(SearchManagerType.COMBINED) | |
return inflater.inflate(R.layout.fragment_maps, container, false) | |
} | |
override fun onStop() { | |
// Activity onStop call must be passed to both MapView and MapKit instance. | |
mapview.onStop() | |
MapKitFactory.getInstance().onStop() | |
super.onStop() | |
} | |
override fun onStart() { | |
// Activity onStart call must be passed to both MapView and MapKit instance. | |
super.onStart() | |
MapKitFactory.getInstance().onStart() | |
mapview.onStart() | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
mapObjects = mapview.getMap().getMapObjects().addCollection() | |
// And to show what can be done with it, we move the camera to the center of Saint Petersburg. | |
// And to show what can be done with it, we move the camera to the center of Saint Petersburg. | |
val center = AlaketApp.getInstance().lastLocation?.let { | |
LonLat(it.latitude, it.longitude) | |
} ?: lonLatOfBishkek | |
val targetLocation = Point(center.latitute, center.longtitute) | |
mapview.map.move( | |
CameraPosition(targetLocation, 14.0f, 0.0f, 0.0f) | |
) | |
mapview.map.addCameraListener(this) | |
initMapsFragment(view) | |
observeViewModel() | |
createMapObjects() | |
subscribeToDeviceLocation() | |
requireActivity().onBackPressedDispatcher.addCallback(this) { | |
requireActivity().finish() | |
} | |
} | |
private fun createMapObjects() { | |
val mark = mapObjects!!.addPlacemark(DRAGGABLE_PLACEMARK_CENTER) | |
mark.opacity = 0.5f | |
mark.setIcon(ImageProvider.fromResource(requireContext(), R.drawable.ic_point_a)) | |
mark.isDraggable = true | |
} | |
fun observeViewModel() { | |
val lifeOwner = this as LifecycleOwner | |
with(viewModel) { | |
currentPickedLocation.observe(lifeOwner, Observer { | |
it?.let { | |
tv_location.text = it | |
showDisplayFragment() | |
setPickerIcon() | |
} | |
}) | |
moveMapToLonLat.observe(lifeOwner, Observer { | |
it?.let { | |
// And to show what can be done with it, we move the camera to the center of Saint Petersburg. | |
mapview.getMap().move( | |
CameraPosition( | |
Point( | |
it.latitute, | |
it.longtitute | |
), 14.0f, 0.0f, 0.0f | |
) | |
) | |
} | |
}) | |
bottomSheetExpanded.observe(lifeOwner, Observer { opened -> | |
if (opened == false && inInitialState.value == true) { | |
// markerPointA?.let { map?.hideMarker(it) } | |
// markerPointB?.let { map?.hideMarker(it) } | |
} | |
}) | |
} | |
} | |
private fun setPickerIcon() { | |
val currentIsPointA = viewModel.currentIsPickupLocation | |
iv_picker.setImageResource( | |
if (currentIsPointA) R.drawable.ic_point_a else R.drawable.ic_point_b | |
) | |
} | |
private fun addLocationBMarker(lonLat: LonLat) { | |
mapview.getMap().move( | |
CameraPosition( | |
Point( | |
lonLat.latitute, | |
lonLat.longtitute | |
), 14.0f, 0.0f, 0.0f | |
) | |
) | |
val mark = mapObjects!!.addPlacemark(Point(lonLat.latitute, lonLat.longtitute)) | |
mark.opacity = 0.5f | |
mark.setIcon(ImageProvider.fromResource(requireContext(), R.drawable.mark)) | |
mark.isDraggable = false | |
} | |
@SuppressLint("ClickableViewAccessibility") | |
private fun initMapsFragment(view: View) { | |
mapOf( | |
R.id.zoom_in to this::zoomInMap, | |
R.id.zoom_out to this::zoomOutMap, | |
R.id.location to this::centerMap | |
).forEach { | |
val btn = view.findViewById<ImageButton>(it.key) | |
btn.setOnClickListener(it.value) | |
} | |
} | |
private fun subscribeToDeviceLocation() { | |
context?.let { | |
it.locationLiveData.observe(viewLifecycleOwner, Observer { location -> | |
if (location != null) { | |
// onUserLocationChanged(LonLat(location.longitude, location.latitude)) | |
} | |
}) | |
} | |
} | |
private fun centerMap(@Suppress("UNUSED_PARAMETER") view: View?) { | |
val location = AlaketApp.getInstance().lastLocation ?: return | |
mapview.getMap().move( | |
CameraPosition(Point(location.latitude, location.longitude), 15.0f, 0.0f, 0.0f) | |
) | |
} | |
private fun zoomInMap(@Suppress("UNUSED_PARAMETER") view: View?) { | |
val lCameraPosition = CameraPosition( | |
mapview.getMapWindow().getMap().getCameraPosition().getTarget(), | |
mapview.getMapWindow().getMap().getCameraPosition().getZoom() + 1, | |
0f, | |
0f | |
) | |
val lAnimation = Animation(Animation.Type.SMOOTH, 0.3f) | |
mapview.getMapWindow().getMap().move(lCameraPosition, lAnimation, null) | |
} | |
private fun zoomOutMap(@Suppress("UNUSED_PARAMETER") view: View?) { | |
val lCameraPosition = CameraPosition( | |
mapview.getMapWindow().getMap().getCameraPosition().getTarget(), | |
mapview.getMapWindow().getMap().getCameraPosition().getZoom() - 1, | |
0f, | |
0f | |
) | |
val lAnimation = Animation(Animation.Type.SMOOTH, 0.3f) | |
mapview.getMapWindow().getMap().move(lCameraPosition, lAnimation, null) | |
} | |
override fun onCameraPositionChanged( | |
map: Map, | |
cameraPosition: CameraPosition, | |
p2: CameraUpdateReason, | |
p3: Boolean | |
) { | |
Log.d("NursTag", "onCameraPositionChanged") | |
SearchFactory.getInstance().createSearchManager(SearchManagerType.COMBINED).submit( | |
Point( | |
cameraPosition.target.latitude, | |
cameraPosition.target.longitude | |
), | |
18, | |
SearchOptions().setSearchTypes(SearchType.GEO.value), | |
object : Session.SearchListener { | |
override fun onSearchResponse(response: Response) { | |
val street = response.collection.children.firstOrNull()?.obj | |
?.metadataContainer | |
?.getItem(ToponymObjectMetadata::class.java) | |
?.address | |
?.components | |
?.firstOrNull { it.kinds.contains(Address.Component.Kind.STREET) } | |
?.name | |
val home = response.collection.children.firstOrNull()?.obj | |
?.metadataContainer | |
?.getItem(ToponymObjectMetadata::class.java) | |
?.address | |
?.components | |
?.firstOrNull { it.kinds.contains(Address.Component.Kind.HOUSE) } | |
?.name | |
Log.d("NursTag", "${street.toString()} ${home.toString()}") | |
val lonLat = LonLat( | |
cameraPosition.target.latitude, | |
cameraPosition.target.longitude | |
) | |
viewModel.updateCurrentPickedLocation("$street $home",lonLat) | |
} | |
override fun onSearchError(error: Error) {} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment