符号图层图标大小改变 Mapbox - Android不工作。

问题描述 投票:0回答:1

我想做一个例子,当我点击一个符号层时,它将被展开,以知道哪个符号层被按下。我输入的数据是通过两个 geojson 文件,我用Mapbox Studio创建的。我试图按照这个例子https:/docs.mapbox.comandroidmapsexamplesicon-size-change-on-click。 但不是都没有放大,就是同色的都放大了(同一层)。有什么办法吗?我到底做错了什么?目前我只是尝试缩放那些带有 "first-layer-id "的图层。非常感谢您。

我的代码在这里。

package com.novadev.mapboxexample.marker

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Feature
import com.mapbox.geojson.FeatureCollection
import com.mapbox.mapboxsdk.Mapbox
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.layers.Property
import com.mapbox.mapboxsdk.style.layers.PropertyFactory
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import com.novadev.mapboxexample.R
import kotlinx.android.synthetic.main.activity_marker.*
import java.net.URI
import java.net.URISyntaxException


class MarkerGeojson : AppCompatActivity(),
    OnMapReadyCallback,
    MapboxMap.OnMapClickListener {

    private lateinit var mapboxMap: MapboxMap
    private lateinit var markerAnimator: ValueAnimator
    private var markerSelected = false



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Mapbox access token is configured here. This needs to be called either in your application
        // object or in the same activity which contains the mapview.
        Mapbox.getInstance(this, getString(R.string.map_box_auth_key))
        // This contains the MapView in XML and needs to be called after the access token is configured.
        setContentView(R.layout.activity_marker)
        mapView.onCreate(savedInstanceState)
        mapView.getMapAsync(this)
        initListeners()

    }

    override fun onMapReady(mapboxMap: MapboxMap) {
        this.mapboxMap = mapboxMap
        getMap()
        mapboxMap.addOnMapClickListener(this)
    }

    override fun onMapClick(point: LatLng): Boolean {
        val pixel = mapboxMap.projection.toScreenLocation(point)
        val features = mapboxMap.queryRenderedFeatures(pixel,"first-layer-id")
        val selectedFeature = mapboxMap.queryRenderedFeatures(
            pixel, "selected-marker-layer"
        )

        mapboxMap.getStyle{ style->
            val selectedMarkerSymbolLayer =
                (style.getLayer("selected-marker-layer") as SymbolLayer)




            if (selectedFeature.size > 0 && markerSelected) false

            if (features.isEmpty()) if (markerSelected) {
                deselectMarker(selectedMarkerSymbolLayer)
            }else false

            val source: GeoJsonSource? = style.getSourceAs("selected-marker")
            source?.setGeoJson(
                FeatureCollection.fromFeatures(
                    arrayOf(
                        Feature.fromGeometry(
                            features[0].geometry()
                        )
                    )
                )
            )

            if (markerSelected) {
                deselectMarker(selectedMarkerSymbolLayer)
            }
            if (features.size > 0) {
                selectMarker(selectedMarkerSymbolLayer)
            }
            // Get the first feature within the list if one exist
            if (features.size > 0) {
                val feature = features[0]

                // Ensure the feature has properties defined
                for ((key, value) in feature.properties()!!.entrySet()) {
                    // Log all the properties
                    Log.d("TAG", String.format("%s = %s", key, value))
                    when (key) {
                        "NOMBRE" -> {
                            tvTitleMarker.text = value.toString()
                            cvInfo.visibility = View.VISIBLE
                        }
                        "TELEFONO" -> tvSubtitlemarker.text = value.toString()
                    }


                }

            }
        }

        return true
    }

    private fun initListeners() {
        ivClose.setOnClickListener {
            cvInfo.visibility = View.GONE
        }
    }

    private fun getMap() {
        mapboxMap.setStyle(
            Style.MAPBOX_STREETS
        ) {
            // Map is set up and the style has loaded. Now you can add data or make other map adjustments.
            geoJSONToMap(
                "first-source-id",
                "first-layer-id",
                "asset://madridmujeres.geojson",
                it
            )
            geoJSONToMap(
                "second-source-id",
                "second-layer-id",
                "asset://madridoficinascorreos.geojson",
                it
            )
        }
    }


    private fun drawableToBitmap (drawable : Drawable): Bitmap {
        if (drawable is BitmapDrawable) {
            return drawable.bitmap
        }

        var  bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
        var canvas =  Canvas(bitmap)
        drawable.setBounds(0, 0, canvas.width, canvas.height)
        drawable.draw(canvas)

        return bitmap
    }



    private fun geoJSONToMap(
        sourceId: String,
        layerId: String,
        asset_id: String,
        style: Style) {
        try {
            val source = GeoJsonSource(sourceId, URI(asset_id))
            style.addSource(source)
            if (layerId == "first-layer-id") {
                var icon = drawableToBitmap(this.resources.getDrawable(R.drawable.ic_location_purple))

                style.addImage("img", icon)
                val symbolLayer = SymbolLayer(layerId, sourceId)
                symbolLayer.withProperties(
                    PropertyFactory.iconImage("img"),
                    PropertyFactory.iconAllowOverlap(true),
                    PropertyFactory.iconOffset(arrayOf(0f, -9f)),
                    PropertyFactory.iconAnchor(Property.ICON_ANCHOR_BOTTOM),
                    PropertyFactory.iconIgnorePlacement(true)
                )
                style.addLayer(symbolLayer)

                val sourceMarker = GeoJsonSource("selected-marker")
                style.addSource(sourceMarker)
                val symbolLayerSelected = SymbolLayer("selected-marker-layer", "selected-marker")
                symbolLayerSelected.withProperties(
                    PropertyFactory.iconImage("img"),
                    PropertyFactory.iconAllowOverlap(true),
                    PropertyFactory.iconOffset(arrayOf(0f, -9f)),
                    PropertyFactory.iconAnchor(Property.ICON_ANCHOR_BOTTOM),
                    PropertyFactory.iconIgnorePlacement(true))
                style.addLayer(symbolLayerSelected)

            } else {
                style.addImage("$layerId marker", this.resources.getDrawable(R.drawable.ic_location_yellow))
                val symbolLayer = SymbolLayer(layerId, sourceId)
                symbolLayer.setProperties(
                    PropertyFactory.iconImage("$layerId marker"),
                    PropertyFactory.iconAllowOverlap(true),
                    PropertyFactory.iconAnchor(Property.ICON_ANCHOR_BOTTOM),
                    PropertyFactory.iconIgnorePlacement(true)
                )
                style.addLayer(symbolLayer)
            }

        } catch (e: URISyntaxException) {
            e.printStackTrace()
        }

    }

    private fun selectMarker(iconLayer: SymbolLayer) {
        markerAnimator = ValueAnimator()
        markerAnimator.setObjectValues(1f, 2f)
        markerAnimator.duration = 300
        markerAnimator.addUpdateListener { animator ->
            iconLayer.setProperties(
                PropertyFactory.iconSize(animator.animatedValue as Float)
            )
        }
        markerAnimator.start()
        markerSelected = true
    }

    private fun deselectMarker(iconLayer: SymbolLayer) {
        markerAnimator.setObjectValues(2f, 1f)
        markerAnimator.duration = 300
        markerAnimator.addUpdateListener { animator ->
            iconLayer.setProperties(
                PropertyFactory.iconSize(animator.animatedValue as Float)
            )
        }
        markerAnimator.start()
        markerSelected = false
    }


    // Add the mapView lifecycle to the activity's lifecycle methods
    public override fun onResume() {
        super.onResume()
        mapView!!.onResume()
    }

    override fun onStart() {
        super.onStart()
        mapView!!.onStart()
    }

    override fun onStop() {
        super.onStop()
        mapView!!.onStop()
    }

    public override fun onPause() {
        super.onPause()
        mapView!!.onPause()
    }

    override fun onLowMemory() {
        super.onLowMemory()
        mapView!!.onLowMemory()
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView!!.onDestroy()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapView!!.onSaveInstanceState(outState)
    }

}
android geojson mapbox-android
1个回答
0
投票

看起来你好像没有为选定的标记层设置GeoJSONsource。

GeoJsonSource source = style.getSourceAs("selected-marker");
if (source != null) {
source.setGeoJson(FeatureCollection.fromFeatures(
new Feature[]{Feature.fromGeometry(features.get(0).geometry())}));
}

通过不设置geoJSON源,源将保持包含所有标记的FeatureCollection的geoJSON。--> 所有的标记将被展开,而不是只展开你点击的那个标记。

© www.soinside.com 2019 - 2024. All rights reserved.