我想使用.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)))
最后的"标记3"被创建为新的矢量资产。运行时出现此错误:
C:.....\MyApplication3\app\src\main\java\com\example\myapplication3\MapsActivity.kt: (58, 115(: 以下任何函数都不能使用提供的参数调用: 公共开放乐趣getActivity(p0:上下文!,p1:Int,@NonNull p2:意图,p3:int,@Nullable p4:捆绑?(:待定意图!在 android.app.PendingIntent 中定义 public open fun getActivity(p0: Context!, p1: Int, p2: Intent!, p3: Int(: PendingIntent!在 android 中定义.app.PendingIntent
请帮忙!
Full 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))
}
}
您的MapsActivity
应该显示编译时错误,因为您对getActivity()
的调用是错误的。而不是使用getActivity()
使用的this
因为它在里面Activity
.检查以下内容:
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney").icon(bitmapDescriptorFromVector(this, R.drawable. marker3)))