本文概述
- Google Map API介面
- Google Map的类型
在Kotlin Android Google Map固定位置的上一教程中, 我们显示了Google固定位置。
现在, 在本教程中, 我们将显示标记并将其放置在用户的当前位置。要查看用户的当前位置, 我们需要实现一些Google Map接口并在其中调用回调方法。
Google Map API介面
- OnMapRreadyCallback:当在MapFragment对象上设置其实例时, 调用此接口。准备使用地图时, 将调用OnMapReadyCallback接口的onMapReady(GoogleMap)方法。在onMapReady(GoogleMap)方法中, 我们可以添加标记, 侦听器和其他属性。
- LocationListener:当设备位置更改时, 此接口用于接收通知。位置更改后, 将调用LocationListener接口的onLocationChanged(Location)抽象方法。
- GoogleApiClient.ConnectionCallbacks:此接口提供了分别在设备连接和断开连接时调用的onConnected(Bundle)和onConnectionSuspended(int)回调方法。
- GoogleApiClient.OnConnectionFailedListener:此接口提供了回调方法onConnectionFailed(ConnectionResult), 当将设备连接到服务时发生错误时将调用该方法。
Android用Java Map显示当前位置介绍了使用Java显示Google Map用户当前位置的教程。
Google Map的类型 地图API中提供了四种不同类型的Google地图。每个人都有不同的地图视图。这些类型是“普通”, “混合”, “卫星”和“地形”。
- googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
- googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
- googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
- googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
- googleMap.mapType = MAP_TYPE_NONE
- googleMap.mapType = MAP_TYPE_HYBRID
- googleMap.mapType = MAP_TYPE_SATELLITE
- googleMap.mapType = MAP_TYPE_TERRAIN
activity_maps.xml
在activity_maps.xml布局文件中添加以下代码。
<
fragment xmlns:android="http://schemas.android.com/apk/res/android"xmlns:map="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/map"android:name="com.google.android.gms.maps.SupportMapFragment"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="example.srcmini.com.kotlingooglemapcurrentlocation.MapsActivity" />
strings.xml
<
resources>
<
string name="app_name">
Kotlin Google Map Current Location<
/string>
<
string name="title_activity_maps">
Map Current Location<
/string>
<
/resources>
build.gradle
在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_maps_api.xml
将Google Map API密钥放置在res / values / google_map_api.xml文件中。
<
resources>
<
!--https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=20:0B:71:3B:B2:46:75:A1:87:78:2E:4C:49:3F:E3:B6:FD:2D:76:D3%3Bexample.srcmini.com.kotlingooglemapcurrentlocationAlternatively, follow the directions here:https://developers.google.com/maps/documentation/android/start#get-key-->
<
string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">
AIzaSyCuxsZ0D73o-REPLACE-WITH-YOUR-API<
/string>
<
/resources>
MapsActivity.kt
在MapsActivity.kt类文件中添加以下代码。
package example.srcmini.com.kotlingooglemapcurrentlocationimport android.content.pm.PackageManagerimport android.location.Locationimport android.os.Buildimport android.os.Bundleimport android.support.v4.app.FragmentActivityimport android.support.v4.content.ContextCompatimport com.google.android.gms.common.ConnectionResultimport com.google.android.gms.common.api.GoogleApiClientimport com.google.android.gms.maps.CameraUpdateFactoryimport com.google.android.gms.maps.GoogleMapimport com.google.android.gms.maps.OnMapReadyCallbackimport com.google.android.gms.maps.SupportMapFragmentimport com.google.android.gms.maps.model.BitmapDescriptorFactoryimport com.google.android.gms.maps.model.LatLngimport com.google.android.gms.maps.model.Markerimport com.google.android.gms.maps.model.MarkerOptionsimport android.Manifestimport android.os.Looperimport android.widget.Toastimport com.google.android.gms.location.*import com.google.android.gms.location.FusedLocationProviderClientimport com.google.android.gms.location.LocationCallbackimport com.google.android.gms.location.LocationRequestimport com.google.android.gms.location.LocationServicesclass MapsActivity : FragmentActivity(), OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {private var mMap: GoogleMap? = nullinternal lateinit var mLastLocation: Locationinternal lateinit var mLocationResult: LocationRequestinternal lateinit var mLocationCallback: LocationCallbackinternal var mCurrLocationMarker: Marker? = nullinternal var mGoogleApiClient: GoogleApiClient? = nullinternal lateinit var mLocationRequest: LocationRequestinternal var mFusedLocationClient: FusedLocationProviderClient? = nulloverride 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 SupportMapFragmentmapFragment.getMapAsync(this)}override fun onMapReady(googleMap: GoogleMap) {mMap = googleMapif (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}}@Synchronizedprotected 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 = 1000mLocationRequest.fastestInterval = 1000mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACYif (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 = locationif (mCurrLocationMarker != null) {mCurrLocationMarker!!.remove()}//Place current location markerval 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 cameramMap!!.moveCamera(CameraUpdateFactory.newLatLng(latLng))mMap!!.animateCamera(CameraUpdateFactory.zoomTo(11f))//stop location updatesif (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()}}
AndroidManifest.xml
<
?xml version="1.0" encoding="utf-8"?>
<
manifest xmlns:android="http://schemas.android.com/apk/res/android"package="example.srcmini.com.kotlingooglemapcurrentlocation">
<
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<
applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme">
<
meta-dataandroid:name="com.google.android.geo.API_KEY"android:value="http://www.srcmini.com/@string/google_maps_key" />
<
activityandroid:name=".MapsActivity"android:label="@string/title_activity_maps">
<
intent-filter>
<
action android:name="android.intent.action.MAIN" />
<
category android:name="android.intent.category.LAUNCHER" />
<
/intent-filter>
<
/activity>
<
/application>
<
/manifest>
【Kotlin Android Google Map当前位置】输出:
注意:请在实际的Android设备上进行检查, 以获取最佳输出结果。
文章图片
推荐阅读
- Kotlin Android媒体播放器
- Kotlin Android隐式Intent
- 使用URL的Kotlin Android JSON解析
- Kotlin Android Google Map固定位置
- Kotlin Android Google地图搜索位置
- Android Firebase身份验证-Google登录
- Kotlin Android Google AdMob横幅广告示例
- Kotlin Android Google AdMob非页内广告示例
- Kotlin Android显式Intent