Xamarin Forms Maps 将图像动态添加到 InfoWindow

2024-03-15

我已经从官方网站的示例中实现了自定义渲染器,但我确实需要每个引脚发送不同的图像。图像将通过 API 作为 base64 字符串传递。我真的需要 Android 和 iOS 的实现。

案例场景: 我正在将 CustomPins 加载到地图上。自定义 Pin 图具有不同的字段,例如:高度、宽度、参与者数量、图片1、图片2

例如我现在拥有的:

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace MyApp.Droid.Renderers
{
    public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
    {
        List<CustomPin> customPins;

        public CustomMapRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                NativeMap.InfoWindowClick -= OnInfoWindowClick;
            }

            if (e.NewElement != null)
            {
                var formsMap = (CustomMap)e.NewElement;
                customPins = formsMap.CustomPins;
            }
        }

        protected override void OnMapReady(GoogleMap map)
        {
            base.OnMapReady(map);

            NativeMap.InfoWindowClick += OnInfoWindowClick;
            NativeMap.SetInfoWindowAdapter(this);
        }

        protected override MarkerOptions CreateMarker(Pin pin)
        {
            var marker = new MarkerOptions();
            marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
            marker.SetTitle(pin.Label);
            marker.SetSnippet(pin.Address);
            marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
            return marker;
        }

        void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
        {
            var customPin = GetCustomPin(e.Marker);
            if (customPin == null)
            {
                throw new Exception("Custom pin not found");
            }

            if (!string.IsNullOrWhiteSpace(customPin.Url))
            {
                var url = Android.Net.Uri.Parse(customPin.Url);
                var intent = new Intent(Intent.ActionView, url);
                intent.AddFlags(ActivityFlags.NewTask);
                Android.App.Application.Context.StartActivity(intent);
            }
        }

        public Android.Views.View GetInfoContents(Marker marker)
        {
            var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
            if (inflater != null)
            {
                Android.Views.View view;


                var customPin = GetCustomPin(marker);
                if (customPin == null)
                {
                    throw new Exception("Custom pin not found");
                }

                if (customPin.Name.Equals("Xamarin"))
                {
                    view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
                }
                else
                {
                    view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
                }

                var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
                var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);

                if (infoTitle != null)
                {
                    infoTitle.Text = marker.Title;
                }
                if (infoSubtitle != null)
                {
                    infoSubtitle.Text = marker.Snippet;
                }

                return view;
            }
            return null;
        }

        public Android.Views.View GetInfoWindow(Marker marker)
        {
            return null;
        }

        CustomPin GetCustomPin(Marker annotation)
        {
            var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
            foreach (var pin in customPins)
            {
                if (pin.Position == position)
                {
                    return pin;
                }
            }
            return null;
        }
    }
}

现在,我从布局文件夹(xml 结构)中展示我的信息窗口:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/xamarin" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/monkey" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="10dp">
            <TextView
                android:id="@+id/InfoWindowTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="InfoWindowTitle"
                android:textColor="@android:color/black"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/InfoWindowSubtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="InfoWindowSubtitle"
                android:textColor="@android:color/black" />
        </LinearLayout>
        <ImageButton
            android:id="@+id/InfoWindowButton"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:src="@drawable/info" />
    </LinearLayout>
</LinearLayout>

那么,有没有一种方法可以在运行时将资源图像更改为其他来源?或者我需要做一些不同的事情?

这是 Android 的示例,我也需要 iOS 的渲染器示例

更新: 有没有办法动态生成布局?因为我有不同类型的引脚 - 每种类型都有不同的信息窗口


从共享代码中,您还可以更改资源图像GetInfoContents运行时中的方法。

var formsMap;
...
formsMap = (CustomMap)e.NewElement;
...  

public Android.Views.View GetInfoContents(Marker marker)
{
    var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
    if (inflater != null)
    {
        Android.Views.View view;


        var customPin = GetCustomPin(marker);
        if (customPin == null)
        {
            throw new Exception("Custom pin not found");
        }

        if (customPin.Name.Equals("Xamarin"))
        {
            view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
        }
        else
        {
            view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
        }

        var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
        var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);
        // init imageButton here
        var imageButton =  view.FindViewById<ImageButton>(Resource.Id.InfoWindowButton);

        if (infoTitle != null)
        {
            infoTitle.Text = marker.Title;
        }
        if (infoSubtitle != null)
        {
            infoSubtitle.Text = marker.Snippet;
        }
        if (imageButton != null)
        {
            var resourcePath = formsMap.ImageSourcePath;
            if (resourcePath == "1"){
               imageButton.SetImageResource(Resource.Drawable.IconOne);
            }else if (resourcePath == "1"){
               imageButton.SetImageResource(Resource.Drawable.IconTwo);
            }
            // or use url image source 
            var imageBitmap = GetImageBitmapFromUrl(resourcePath);
            imageButton.SetImageBitmap(imageBitmap);
        }

        return view;
    }
    return null;
}

那需要自定义地图声明一个ImageSourcePath属性,无论要加载资源图片 or 网址图片,他们都可以做到这一点。

public class CustomMap : Map
{
    ...
    
    public static readonly BindableProperty ImageSourcePathProperty = BindableProperty.Create("ImageSourcePath", typeof(string), typeof(CustomMap), null);

    public string ImageSourcePath
    {
        get { return (string)GetValue(ImageSourcePathProperty); }
        set { SetValue(ImageSourcePathProperty, value); }
    }

    ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Xamarin Forms Maps 将图像动态添加到 InfoWindow 的相关文章

  • 计算Android设备的PPI

    如何计算 Android 设备的 PPI 最特别的是 Android 平板电脑 请注意 我想计算设备的 PPI 而不是 DPI 就像一二三一样简单 让我们来计算一下PPI to Nexus 5 例如 float LCD Diagonal 4
  • onBeaconServiceConnect 未调用

    和以前一样 我使用 Android Beacon 库 它已经工作了 我可以通过 BLE 低功耗蓝牙找到信标 但是现在 更新到最新版本的库后 现在方法onBeaconServiceConnect 不再跑了 请告诉我我需要做什么才能让它发挥作用
  • 使用 google Directions API 的地图视图绘制方向 - 解码折线

    我正在尝试使用 Google 方向 API 在我的地图视图上显示方向 但我在从 JSON 响应获取数据时遇到困难 我可以获得 级别 和 点 字符串 但无法弄清楚如何将它们解码为地图上的点 任何帮助将非常感激 我有一个类可以为您解码它们 添加
  • Android 在打开应用程序时会广播吗?

    例如 如果我想知道Youtube何时打开 是否有与之相关的广播 我当然知道我可以轮询 logcat 消息来检查活动 但我可以通过广播来做到这一点吗 因为它会少得多的耗电 此链接似乎表明这是不可能的 如何跟踪 Android 中的应用程序使用
  • Twitter 登录说明

    我想在 Android 中创建一个 Twitter 应用程序 为此 我想创建一个登录页面并登录到 Twitter 为此 我们需要消费者密钥和消费者密钥 这是什么意思 要创建此登录页面 除了 Twitter 帐户之外 我们还需要其他任何东西吗
  • Manifest Merger工具:替换失败

    我正在使用一个使用自己的 android theme 的库 因此在构建时收到以下错误 错误 55 9 任务 contacit processDebugManifest 执行失败 清单合并失败 AndroidManifest xml 中的属性
  • Android 上的 SVG 支持

    Android 支持 SVG 吗 有什么例子吗 最完整的答案是这样的 Android 2 x 默认浏览器本身不支持 SVG Android 3 默认浏览器支持 SVG 要将 SVG 支持添加到 2 x 版本的平台 您有两个基本选择 安装功能
  • 如何使用Android opencv使图像的白色部分透明

    我无法链接超过 2 个网址 因此我将我的照片发布到此博客 请在这里查看我的问题 http blog naver com mail1001 220650041897 http blog naver com mail1001 220650041
  • 播放 SoundCloud 曲目

    我可以在 Android 应用程序中播放 SoundCloud 中的曲目吗 我正在尝试这段代码 但它不起作用 String res https api soundcloud com tracks 84973999 stream client
  • 如何解决 Firebase AuthUi 中无法找到显式活动?

    使用 firebase UI 时 我无法找到显式活动类 com firebase ui auth KickoffActivity protected void onCreate Bundle savedInstanceState super
  • AudioTrack、SoundPool 或 MediaPlayer,我应该使用哪个?

    如果我需要能够 播放多个音频文件 具有不同的持续时间 例如 5 到 30 秒 独立设置右 左声道的音量 应用声音效果 如混响 失真 那么 我应该使用哪个 API 另外 我在 AudioTrack API 上找不到太多文档 有谁知道在哪里可以
  • 放置在 NavigationDrawer 顶部的片段

    我正在尝试添加一个PreferenceFragment在我的应用程序中 问题是 它自动放置在我的顶部NavigationDrawer public class SetPreferenceActivity extends Activity O
  • 如何检查 Android 中连接的 wifi 网络是否处于活动状态

    如何自动检查android中连接的WiFi网络上的互联网是否处于活动状态 我可以检查 wifi 是否已启用或 wifi 网络是否已连接 但我不确定如何检查互联网是否已连接 这可能吗 private boolean connectionAva
  • 如何在TableLayout中创建三列

    我正在开发一个使用的屏幕TableLayout 在这里我可以轻松创建两列 但我怎样才能创建三列呢 这里有一个例子
  • 在游戏视图下添加 admob

    我一直试图将 admob 放在我的游戏视图下 这是我的代码 public class HoodStarGame extends AndroidApplication Override public void onCreate Bundle
  • HERE 地图:更改路线已行驶部分的颜色

    导航时可以改变路线的颜色吗 具体来说 我希望路线中已行驶的部分的颜色与即将行驶的部分的颜色不同 现在都是同一个颜色 将 MapRoute 对象的 TravelColor 变量设置为透明对我来说很有效 mapRoute color Resou
  • 如何在android asynctask中使用inputstream作为参数?

    我正在制作一个 Android 应用程序来跟踪股票详细信息 我将通过 csv 雅虎财经 检索数据 据我所知 在android 4 0中 网络连接无法在主线程上完成 因此 我将使用 asynctask 来建立连接 但是 我在参数方面遇到了一些
  • Android:监听状态栏通知

    有没有办法在状态栏被下拉时监听通知 1 用于检测状态栏变化 您可以注册一个监听器来获取系统UI可见性变化的通知 因此 要在您的活动中注册侦听器 Detecting if the user swipe from the top down to
  • 如何在基本活动中使用 ViewBinding 的抽象?

    我正在创建一个基类 以便子级的所有绑定都将设置在基类中 我已经做到了这一点 abstract class BaseActivity2 b AppCompatActivity private var viewBinding B null pr
  • Android 材料芯片组件崩溃应用程序。无法膨胀 xml

    Tried Chip来自两个支持库的组件 com google android support design 28 0 0 rc01和材料 com google android material material 1 0 0 rc01 堆栈

随机推荐

  • 转义注释标签内的 html

    转义 html 没问题 它会删除 lt s and gt s etc 我遇到了一个问题 我在注释标签内输出文件名 例如 当然 如果你不逃避 事情可能会很糟糕 所以它变成了 问题是 如果文件名称中包含 则所有 html 都会被搞砸 因为不允许
  • didMove(查看: SKView) 和 didMoveToView(查看: SKView) 有什么区别?

    如标题所示 didMove to view SKView 和 didMoveToView view SKView 有什么区别 我知道 didMoveToView 是方法 并且该视图在旧 版本中属于 SKView 类型 我不明白将 查看 SK
  • iPad iPhone 规模背景图片

    只是想知道是否有其他人经历过 iPad iPhone 缩小背景图像以适应视口的情况 就我而言 我通过 JavaScript 交换背景图像 新的背景图像超宽 适合大型显示器 然而 iPad 正在缩小通过 javascript 添加到 DOM
  • 使用 CSS 剪辑/裁剪背景图像

    我有这个 HTML div lorem ipsum div 使用这个CSS graphic background image url image jpg width 200px height 100px 我应用的背景图像是 200x100
  • 有没有办法在flutter中从iOS中排除包?

    我正在使用仅适用于 Android 的 sms maintained 包 然而 该项目需要一个iOS版本 我目前正在做的是在开发iOS时删除该包 如何将包保留在 pubspec yaml 文件中 但禁止 iOS 检入该包 目标是拥有统一的代
  • PHP 中的 \x00 、 \x04 是什么意思

    我有一些代码有 x00 and x04 十六进制代码 这是什么意思 str implode x00 var message line 1 id var message x04 id line 2 将会发生什么线路 1 和线路 2我想将这些写
  • 为什么数组是协变的,而泛型是不变的?

    来自 Joshua Bloch 的 Effective Java 数组与泛型类型有两个重要的不同之处 第一个数组是协变的 泛型是不变的 协变仅仅意味着如果 X 是 Y 的子类型 那么 X 也将是 Y 的子类型 数组是协变的 因为字符串是对象
  • 根据行逐步对 Numpy Python 矩阵进行排序

    我四处寻找并试图找到解决这个看似简单问题的方法 但一无所获 问题是根据矩阵的列逐步对矩阵进行排序 所以 如果我有一个像这样的 numpy 矩阵 import numpy as np X np matrix 0 0 1 2 0 0 1 1 0
  • 是否可以使 eclipse p2 配置机制在*本地*运行?

    Eclipse 3 4 x 也称为Ganymede http www eclipse org downloads packages 附带了一种新的配置机制 称为p2 配置 是允许按需发现和更新应用程序的某些部分的过程 正如本文中关于太阳网站
  • System.Security.SecurityException:主体权限请求失败

    任何人都可以帮助我指出如何修复此错误的正确方向吗 System Web Services Protocols SoapException Server was unable to process request gt System Secu
  • 获取kivy中选定复选框的值

    test py import sqlite3 as lite from kivy uix screenmanager import Screen from kivy app import App from kivy lang import
  • Rsync 仅创建符号链接

    我目前 rsync 运行良好 它将我的所有文件从一个目录复制到另一个目录 唯一的事情是它是物理复制文件 我有很多大文件 我不想拥有所有文件的副本 我只想在新目录中创建一个符号链接 以便我可以在网页上提供数据 源目录中有一些我不希望公众看到的
  • 如何识别和删除换行符和空格?

    我正在通过按组件分隔字符串来创建一个 nsmutable 数组 这会导致在数组中插入大量换行符和空格 如何识别和删除它们 for int i 0 i
  • 解码ima4音频格式

    为了减少 iPhone 应用程序的下载大小 我压缩了一些音频文件 具体来说 我在命令行上使用 afconvert 将 wav 格式更改为 caf 格式 带 ima4 压缩 我读了this http www wooji juice com b
  • 带有循环动画的浓缩咖啡冻结视图

    我有一个视图 其中一个元素在无限循环中使用以下内容进行动画处理
  • Django REST Framework 添加一个 ViewSet 作为另一个 ViewSet 上的详细信息

    我有两种模型 一种是盒子 一种是盒子评论 class BoxViewSet viewsets ModelViewSet queryset Box objects all permission classes IsAuthenticated
  • 如何将相机附加到 Spark.components.VideoDisplay

    我正在使用 Flash Builder 并创建了一个 Spark 应用程序 Flex 项目 该项目将从本地摄像头传输视频 如果我使用mx controls VideoDisplay 没有问题 因为它有attachCamera camera
  • 使用 QEMU 模拟 Big Endian ARM 系统

    是否可以编译一些 Linux 内核并通过 QEMU 运行它 模拟一些 Big Endian ARM 处理器 如果 QEMU 无法做到这一点 我很想知道其他可以做到这一点的系统模拟器 我的基本目标是在尽可能多的本机环境中运行和调试专用的 Bi
  • 从字符串中删除点符号[重复]

    这个问题在这里已经有答案了 可能的重复 如何在 JavaScript 中替换字符串中的所有点 https stackoverflow com questions 2390789 how to replace all points in a
  • Xamarin Forms Maps 将图像动态添加到 InfoWindow

    我已经从官方网站的示例中实现了自定义渲染器 但我确实需要每个引脚发送不同的图像 图像将通过 API 作为 base64 字符串传递 我真的需要 Android 和 iOS 的实现 案例场景 我正在将 CustomPins 加载到地图上 自定