【发布时间】:2020-03-27 11:33:24
【问题描述】:
我想使用 .svg 文件作为地图标记。在 MapsActivity.kt 我写道:
private fun bitmapDescriptorFromVector(context: Context, vectorResId:Int):BitmapDescriptor {
val vectorDrawable = ContextCompat.getDrawable(context, vectorResId);
vectorDrawable!!.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
val bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
val canvas = Canvas(bitmap);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
...还有这个:
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney").icon(bitmapDescriptorFromVector(getActivity(), R.drawable.marker3)))
最后的“marker3”被创建为新的矢量资产。运行时出现此错误:
C:.....\MyApplication3\app\src\main\java\com\example\myapplication3\MapsActivity.kt: (58, 115): 不能使用提供的参数调用以下函数: public open fun getActivity(p0: Context!, p1: Int, @NonNull p2: Intent, p3: Int, @Nullable p4: Bundle?): PendingIntent!在 android.app.PendingIntent 中定义 public open fun getActivity(p0: Context!, p1: Int, p2: Intent!, p3: Int): PendingIntent!在 android.app.PendingIntent 中定义
请帮忙!
完整的 MapsActivity.kt:
package com.example.myapplication3
import android.app.PendingIntent.getActivity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
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.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private fun bitmapDescriptorFromVector(context: Context, vectorResId:Int):BitmapDescriptor {
val vectorDrawable = ContextCompat.getDrawable(context, vectorResId)
vectorDrawable!!.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight())
val bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
vectorDrawable.draw(canvas)
return BitmapDescriptorFactory.fromBitmap(bitmap)
}
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)
}
*/
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
val sydney = LatLng(-34.0, 151.0)
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney").icon(bitmapDescriptorFromVector(getActivity(), R.drawable.marker3)))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
}
【问题讨论】:
-
Exception与VectorDrawable无关,它与PendingIntent相关。仔细检查您的代码。 -
对不起,我没听懂:(
-
添加
MapsActivity.kt的更多详情 -
添加了有问题的完整代码。谢谢!
-
检查我的答案并告诉我
标签: android google-maps android-studio kotlin