📅  最后修改于: 2021-01-05 08:50:36             🧑  作者: Mango
Google Map显示当前位置,导航位置方向,搜索位置等。要将Google Map放置在应用程序中,我们需要创建Google Map API密钥并将其集成到我们的应用程序中。
使用Java代码的Google Map教程是在Android Google Map上实现的。
在本教程中,我们将把Google Maps集成到我们的Android应用程序中。要将Google Map放置在应用程序中,请选择活动类型作为Google Maps Activity 。默认情况下,此活动会生成Google地图所需的必需配置和设置。
要实现Google Map,我们需要生成Google Map API密钥并将其集成到我们的应用程序中。
复制res / values / google_map_api.xml文件中的网址,并将其粘贴到浏览器中,或者我们可以直接在Console Google Developer上访问以生成Google Map API密钥。
单击创建API密钥以生成API密钥。
单击创建API密钥后,它将生成显示以下屏幕的我们的API密钥。
将生成的API密钥粘贴到我们的res / values / google_map_api.xml文件中。
在activity_maps.xml布局文件中添加以下代码。
在build.gradle文件中添加Google Map Service和Google Location Service依赖项。
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
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'
implementation 'com.google.android.gms:play-services-location:11.8.0'
testImplementation 'junit:junit:4.12'
}
Kotlin Google Map
Map Fixed Location
将Google Map API密钥放置在res / values / google_map_api.xml文件中。
AIzaSyCmTF-n-REPLACE-WITH-YOUR-KEY
要在MapsActivity.kt类中获取GoogleMap对象,我们需要实现OnMapReadyCallback接口并重写onMapReady()回调方法。要在地图上显示定位位置,请将纬度和经度点放置在LatLng(纬度,经度)中。
GoogleMap.addMarker()指向给定位置的位置。
package example.javatpoint.com.kotlingooglemap
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
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
class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var mMap: GoogleMap
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)
}
/**
* 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.
*/
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in India and move the camera
val myLocation = LatLng(20.5937, 78.9629)
mMap.addMarker(MarkerOptions().position(myLocation).title("Marker in India"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLocation))
mMap.uiSettings.isZoomControlsEnabled = true
}
override fun onMarkerClick(p0: Marker?) = false
}
添加
输出:
基于Marshmallow版本构建的应用程序有助于运行时用户权限。在此类中,我们通过提供运行时权限访问设备精细位置ACCESS_FINE_LOCATION来创建上述示例。
package example.javatpoint.com.kotlingooglemap
import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.app.FragmentActivity
import android.widget.Toast
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
class MapsActivity : FragmentActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
private lateinit var mMap: GoogleMap
companion object {
private val MY_PERMISSION_FINE_LOCATION = 101
}
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
// Add a marker in India and move the camera
val india = LatLng(20.5937, 78.9629)
mMap.addMarker(MarkerOptions().position(india).title("Marker in India"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(india))
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLocationEnabled = true
}
else {//condition for Marshmello and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_PERMISSION_FINE_LOCATION)
}
}
mMap.setOnMarkerClickListener(this)
}
override fun onMarkerClick(p0: Marker?) = false
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
MY_PERMISSION_FINE_LOCATION -> if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//permission to access location grant
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.isMyLocationEnabled = true
}
}
//permission to access location denied
else {
Toast.makeText(applicationContext, "This app requires location permissions to be granted", Toast.LENGTH_LONG).show()
finish()
}
}
}
}
输出: