📅  最后修改于: 2021-01-05 08:49:19             🧑  作者: Mango
要在Android应用程序中实现Google Map,我们需要生成Google Map API密钥。 Google Map Fixed Location教程中介绍了创建Google Map API密钥的过程。
在Kotlin Android Android Google Map Fixed Location的上一教程中,我们显示了Google的固定位置。
现在,在本教程中,我们将显示标记并将其放置在用户的当前位置。要查看用户的当前位置,我们需要实现一些Google Map接口并在其中调用回调方法。
GoogleMap的isLocationEnabled属性用于启用位置图层,该图层允许设备与当前位置进行交互。
Android用Java Map显示当前位置中介绍了使用Java显示Google Map用户当前位置的教程。
地图API中提供了四种不同类型的Google地图。每个人都有不同的地图视图。这些类型是“普通”,“混合”,“卫星”和“地形”。
使用Kotlin语法,我们将使用以上Google地图类型:
有关这些地图类型的详细信息在此处说明。
在activity_maps.xml布局文件中添加以下代码。
Kotlin Google Map Current Location
Map Current Location
在build.gradle文件中添加Google Map Service和Google Location Service依赖项。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-maps:11.8.0'
compile 'com.google.android.gms:play-services-location:11.8.0'
testImplementation 'junit:junit:4.12'
}
将Google Map API密钥放置在res / values / google_map_api.xml文件中。
AIzaSyCuxsZ0D73o-REPLACE-WITH-YOUR-API
在MapsActivity.kt类文件中添加以下代码。
package example.javatpoint.com.kotlingooglemapcurrentlocation
import android.content.pm.PackageManager
import android.location.Location
import android.os.Build
import android.os.Bundle
import android.support.v4.app.FragmentActivity
import android.support.v4.content.ContextCompat
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.api.GoogleApiClient
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.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import android.Manifest
import android.os.Looper
import android.widget.Toast
import com.google.android.gms.location.*
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationServices
class MapsActivity : FragmentActivity(), OnMapReadyCallback, LocationListener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private var mMap: GoogleMap? = null
internal lateinit var mLastLocation: Location
internal lateinit var mLocationResult: LocationRequest
internal lateinit var mLocationCallback: LocationCallback
internal var mCurrLocationMarker: Marker? = null
internal var mGoogleApiClient: GoogleApiClient? = null
internal lateinit var mLocationRequest: LocationRequest
internal var mFusedLocationClient: FusedLocationProviderClient? = null
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)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient()
mMap!!.isMyLocationEnabled = true
}
} else {
buildGoogleApiClient()
mMap!!.isMyLocationEnabled = true
}
}
@Synchronized
protected fun buildGoogleApiClient() {
mGoogleApiClient = GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build()
mGoogleApiClient!!.connect()
}
override fun onConnected(bundle: Bundle?) {
mLocationRequest = LocationRequest()
mLocationRequest.interval = 1000
mLocationRequest.fastestInterval = 1000
mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
mFusedLocationClient?.requestLocationUpdates(mLocationRequest,mLocationCallback, Looper.myLooper())
}
}
override fun onLocationChanged(location: Location) {
mLastLocation = location
if (mCurrLocationMarker != null) {
mCurrLocationMarker!!.remove()
}
//Place current location marker
val latLng = LatLng(location.latitude, location.longitude)
val markerOptions = MarkerOptions()
markerOptions.position(latLng)
markerOptions.title("Current Position")
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
mCurrLocationMarker = mMap!!.addMarker(markerOptions)
//move map camera
mMap!!.moveCamera(CameraUpdateFactory.newLatLng(latLng))
mMap!!.animateCamera(CameraUpdateFactory.zoomTo(11f))
//stop location updates
if (mGoogleApiClient != null) {
mFusedLocationClient?.removeLocationUpdates(mLocationCallback)
}
}
override fun onConnectionFailed(connectionResult: ConnectionResult) {
Toast.makeText(applicationContext,"connection failed", Toast.LENGTH_SHORT).show()
}
override fun onConnectionSuspended(p0: Int) {
Toast.makeText(applicationContext,"connection suspended", Toast.LENGTH_SHORT).show()
}
}
输出:
注意:请在实际的Android设备上进行检查,以获取最佳输出结果。