【问题标题】:Custom .svg google maps marker in Kotlin, Android StudioKotlin,Android Studio 中的自定义 .svg 谷歌地图标记
【发布时间】: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))
}

}

【问题讨论】:

  • ExceptionVectorDrawable 无关,它与PendingIntent 相关。仔细检查您的代码。
  • 对不起,我没听懂:(
  • 添加MapsActivity.kt的更多详情
  • 添加了有问题的完整代码。谢谢!
  • 检查我的答案并告诉我

标签: android google-maps android-studio kotlin


【解决方案1】:

您的MapsActivity 应该显示编译时错误,因为您对getActivity() 的调用是错误的。而不是使用getActivity(),而是使用this,因为它在Activity内部。检查以下:

mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney").icon(bitmapDescriptorFromVector(this, R.drawable. marker3)))

【讨论】:

  • 不客气@RobertZt,请接受答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 2020-10-02
  • 2016-01-16
  • 2013-08-21
  • 1970-01-01
相关资源
最近更新 更多