Skip to content

Smart helper for marker movement in Google Maps and Mapbox

License

Notifications You must be signed in to change notification settings

utsmannn/SmartMarker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart Marker For Google Maps and Mapbox

This library for helper movement marker in your maps

Table of Content

Download

Download

// the core library
implementation 'com.utsman.smartmarker:core:1.3.2@aar'

// extension for google maps
implementation 'com.utsman.smartmarker:ext-googlemaps:1.3.2@aar'

// extension for Mapbox
implementation 'com.utsman.smartmarker:ext-mapbox:1.3.2@aar'

For extensions, you don't need to add mapbox extensions if you not use the sdk mapbox. As well as the google map sdk.

Add Marker

Google Maps

Use the default method as usual for google maps. Reference for add marker in google maps is here
And code look like this

val markerOption = MarkerOptions()
        .position(latLng)

val marker = map.addMarker(markerOption) // this your marker

Mapbox

For Mapbox, adding marker is little hard, so I create helper for it, and you must coding after setup style

// define marker options
val markerOption = MarkerOptions.Builder() // from 'com.utsman.smartmarker.mapbox.MarkerOptions'
    .setId("marker-id", true) // if marker id need unique id with timestamp, default is false
    .setIcon(R.drawable.ic_marker, true) // if marker is not vector, use 'false'
    .setPosition(latLng)
    .setRotation(rotation)
    .build(context)

// add your marker
val marker = map.addMarker(markerOption)

Move Your Marker

SmartMarker.moveMarkerSmoothly(marker, latLng) 
// or for disable rotation
SmartMarker.moveMarkerSmoothly(marker, latLng, false)

// with extensions for kotlin
marker.moveMarkerSmoothly(latLng)
// or for disable rotation
marker.moveMarkerSmoothly(latLng, false)

Location Watcher Extension

I create location extensions for get your location every second with old location and new location, you can setup realtime level.

Installation

implementation 'com.utsman.smartmarker:ext-location:1.2.5@aar'

// for extensions watcher location, you need some library with latest versions
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'pl.charmas.android:android-reactive-location2:2.1@aar'
implementation 'io.reactivex.rxjava2:rxjava:2.2.12'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.karumi:dexter:6.0.0'

Realtime Level

You can setup realtime level for get every second update, locationWatcher.getLocationUpdate(priority, listener)

level ms
LocationWatcher.Priority.JEDI 3 ms
LocationWatcher.Priority.VERY_HIGH 30 ms
LocationWatcher.Priority.HIGH 50 ms
LocationWatcher.Priority.MEDIUM 300 ms
LocationWatcher.Priority.LOW 3000 ms
LocationWatcher.Priority.VERY_LOW 8000 ms

Use

// define location watcher
val locationWatcher: LocationWatcher = LocationWatcher(context)

// get location once time
locationWatcher.getLocation { location ->
    // your location result
}

// get location update every second
locationWatcher.getLocationUpdate(LocationWatcher.Priority.HIGH, object : LocationUpdateListener {
    override fun oldLocation(oldLocation: Location?) {
        // your location realtime result
    }
    
    override fun newLocation(newLocation: Location?) {
        // your location past with delay 30 millisecond (0.03 second)
    }

    override fun failed(throwable: Throwable?) {
        // if location failed
    }
})

// stop your watcher in onStop activity
override fun onDestroy() {
    locationWatcher.stopLocationWatcher()
    super.onDestroy()
}

Permission helper

If you have not applied location permission for your app, you can be set permission with adding context before listener.

// get location once time with permission helper
locationWatcher.getLocation(context) { location ->
    // your location result
}

// get location update every second with permission helper
locationWatcher.getLocationUpdate(context, LocationWatcher.Priority.HIGH, object : LocationUpdateListener {
    override fun oldLocation(oldLocation: Location?) {
        // your location realtime result
    }
    
    override fun newLocation(newLocation: Location?) {
        // your location past with delay 30 millisecond (0.03 second)
    }

    override fun failed(throwable: Throwable?) {
        // if location failed
    }
})

Don't forget to add location permission android.permission.ACCESS_FINE_LOCATION for your apps

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Other Extensions

// Convert Location to LatLng for Google Maps ('com.google.android.gms.maps.model.LatLng')
location.toLatLngGoogle()

// Convert Location to LatLng for Mapbox ('com.mapbox.mapboxsdk.geometry.LatLng')
location.toLatLngMapbox()

// use marker as vector for Google Maps
bitmapFromVector(context, R.drawable.marker_vector)

Simple Example

Google Maps

class MainActivity : AppCompatActivity() {

    private lateinit var locationWatcher: LocationWatcher
    private var marker: Marker? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        locationWatcher = LocationWatcher(this)
        val googleMapsView = (google_map_view as SupportMapFragment)

        locationWatcher.getLocation(this) { loc ->
            googleMapsView.getMapAsync {  map ->
                val markerOption = MarkerOptions()
                    .position(loc.toLatLngGoogle())
                    .icon(bitmapFromVector(this@MainActivity, R.drawable.ic_marker))

                marker = map.addMarker(markerOption)
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc.toLatLngGoogle(), 17f))
            }
        }

        // device tracker
        locationWatcher.getLocationUpdate(this, LocationWatcher.Priority.HIGH, object : LocationUpdateListener {
            override fun oldLocation(oldLocation: Location) {

            }

            override fun newLocation(newLocation: Location) {
                // move your marker smoothly with new location
                marker?.moveMarkerSmoothly(newLocation.toLatLngGoogle())
                                    
                // or use class SmartMarker for java
                // SmartMarker.moveMarkerSmoothly(marker, newLocation.toLatLngMapbox())    
            }

            override fun failed(throwable: Throwable?) {
            }
        })
    }
}

Mapbox

class MainActivity : AppCompatActivity() {

    private lateinit var locationWatcher: LocationWatcher
    private var marker: Marker? = null // from 'com.utsman.smartmarker.mapbox.Marker'

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // setup instance with api key mapbox before 'setContentView'
        Mapbox.getInstance(this, "sk.eyJ1Ijoia3VjaW5nYXBlcyIsImEiOiJjazI1eXFqYzQxcGZjM25ueTZiMHU3aDl3In0.EfIuu2NSv2CacIKEhkXhCg")
        setContentView(R.layout.activity_main)

        locationWatcher = LocationWatcher(this)
      
        locationWatcher.getLocation(this) { loc ->
            mapbox_view.getMapAsync {  map ->
                
                // set style before setup your marker
                map.setStyle(Style.OUTDOORS) { style -> 
                
                    val markerOption = MarkerOptions.Builder() // from 'com.utsman.smartmarker.mapbox.MarkerOptions'
                           .setId("marker-id")
                           .addIcon(R.drawable.ic_marker, true)
                           .addPosition(loc.toLatLngMapbox())
                           .build(this)
    
                    val markerLayer = map.addMarker(markerOption)
                    marker = markerLayer.get("marker-id")
                    
                    map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc.toLatLngGoogle(), 17f))
                }
            }
        }

        // device tracker
        locationWatcher.getLocationUpdate(this, LocationWatcher.Priority.HIGH, object : LocationUpdateListener {
            override fun oldLocation(oldLocation: Location) {

            }

            override fun newLocation(newLocation: Location) {
                // move your marker smoothly with new location
                marker?.moveMarkerSmoothly(newLocation.toLatLngGoogle())

                // or use class SmartMarker for java
                // SmartMarker.moveMarkerSmoothly(marker, newLocation.toLatLngMapbox())  
            
            }

            override fun failed(throwable: Throwable?) {
            }
        })
    }
}

My Other Libraries

  • Recycling
    A Library for make an easy and faster RecyclerView without adapter

  • rmqa
    Rabbit Message Queue for Android

  • Anko Navigation Drawer
    Library for implementation Navigation Drawer with styles in Anko

  • Easy Google Login
    Library for Simplify Firebase Authenticate Google Auth


Copyright 2019 Muhammad Utsman

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

makasih