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 的相关文章

  • 在 ChromeO 上安装未知来源的 apk

    我今天早上更新了我的 Chromebook Asus Flip 以获取 Play 商店 我的 Chromebook 安装了 M53dev 通道版本 它运作良好 我可以安装并运行从 Play 商店下载的 Android 应用程序 我想测试我的
  • Android 中的 Fragment-Fragment 通信

    我在Android编程方面处于初级水平 所以我需要你真诚的帮助 请任何人帮助我 我正在尝试使用片段构建滑动用户界面 所以我真正的疑问是 我有一个Fragment say FragmentA 它有一个TextView and Button在其
  • 吉夫伦致命信号11

    我正在尝试使用一些本机代码来创建 Gif 我使用绘画绘制图像 创建一些笔画 单击 保存 绘制的图像将保存为 JPG 格式 当我单击 创建 Gif 时 它会获取所有图像并开始创建 gif 这是当我收到致命信号 11 并且应用程序重新启动时 我
  • Fragment 问题中的 ExpandableListView

    我正在尝试在片段中实现可扩展列表视图 没有错误出现 当我尝试记录两个的输出时List
  • Android 全屏对话框确认和拒绝操作

    材料设计中的全屏对话框应该在操作栏 工具栏上有确认和拒绝操作 我的问题是 我该怎么做 显示对话框 getFragmentManager beginTransaction add R id container new MyDialogFrag
  • Android 中 localTime 和 localDate 的替代类有哪些? [复制]

    这个问题在这里已经有答案了 我想使用从 android API 获得的长值 该值将日期返回为长值 表示为自纪元以来的毫秒数 我需要使用像 isBefore plusDays isAfter 这样的方法 Cursor managedCurso
  • 选项卡主机内的 Android Fragment 视图状态 [重复]

    这个问题在这里已经有答案了 可能的重复 使用 Fragment 为 Android 中的每个选项卡单独的返回堆栈 https stackoverflow com questions 6987334 separate back stack f
  • 在新的 intel x86 android 模拟器中访问 google api

    我只是尝试在新的 x86 android 模拟器中运行我公司的应用程序 但是我们的应用程序依赖于 google 地图 API 而这在 google 随 android sdk 版本 17 提供的 x86 系统映像中不可用 我的直觉告诉我答案
  • Android蓝牙java.io.IOException:bt套接字已关闭,读取返回:-1

    我正在尝试编写一个代码 仅连接到运行 Android 5 0 KitKat 的设备上的 目前 唯一配对的设备 无论我尝试了多少方法 我仍然会收到此错误 这是我尝试过的最后一个代码 它似乎完成了我看到人们报告为成功的所有事情 有人能指出我做错
  • 控制 OverlayItem 大小

    我正在构建一个在单个 ItemizedOverlay 中包含几十个 OverlayItems 的地图 我的地图设计为可以非常近距离地查看 大约缩放级别 18 并且 OverlayItems 彼此非常接近 地图放大时看起来不错 但是 如果用户
  • 在android中,将相机预览流到视图上

    我想将 Android 相机的相机预览流式传输到视图上 目的是随后使用 onDraw 将各种内容添加到视图中 我不需要随时实际捕捉图像 它不必是最高质量或每秒最大数量的帧 有谁知道如何做到这一点 将其添加到您的 xml 中
  • Android 上的 MIDI:Java 和/或 AIR 库

    一段时间以来 我一直在考虑在 iPad 上 重新 构建一个应用程序 其中我将使用 Objective C 和DSMI http dsmi tobw net 将 MIDI 信号发送到主机 这还不错 我的意思是 除了实际编写应用程序之外 现在我
  • 安卓。 CalendarView...一次仅显示一个月的日历

    我正在使用 CalendarView 其中我想一次仅查看一个月的日历并滚动查看下个月 但 CalendarView 一次显示所有月份 下面是我的代码
  • 使用 Play Integrity API 时,Firebase 电话身份验证会出现缺少客户端标识符错误

    使用 Firebase 电话身份验证注册 登录时 身份验证流程始终会启动 reCAPTCHA 流程 并在返回应用程序后发出missing client identifier error 我的设置之前适用于设备验证 安全网络 API 除了我的
  • Android 中的库可以有自己的意图过滤器吗?

    我想开发一个可以包含在其他 Android 应用程序中的库来拦截某些类型的意图 是否可以 我创建了一个库和一个测试项目 两者都有自己的AndroidManifest xml文件 在库的清单中 我为操作 TEST 定义了一个意图过滤器 但是
  • 哪个视图最亮?

    在Android中 哪个是轻量级视图 例如 View Textview Edittext 等 在某些情况下 我们需要使用视图来填充区域而不向用户显示视图 同时屏幕加载速度应该很快 您可以使用空间 android widget Space S
  • Android SearchView 在启动时隐藏键盘

    我有一个小问题正在尝试解决 当我打开应用程序时 键盘会显示输入搜索视图的查询 不过 我只想在单击搜索视图时显示键盘 我该如何解决 Thanks 这对我有用 用于隐藏焦点的代码 searchView SearchView view findV
  • LifeCycleAware Fragment 中的片段生命周期事件

    我有一个生命周期感知片段和一个LifecycleObserver class public class MyFragment extends Fragment Override public void onCreate Nullable B
  • 如何访问我的 Android 程序中的联系人

    我正在制作一个短信应用程序 并且想要访问我的 Android 应用程序中的联系人 我想访问联系人 就像他们在实际联系人列表中一样 选择后 我需要返回到我的活动 在其中我可以向该人发送短信 或者是否可以访问存储联系人的数据库 我的代码如下所示
  • Android GetPTLAFormat 上的 Phonegap 错误

    我们正在开发一个使用 jQuery 移动和电话间隙的应用程序 一切似乎都工作正常 但是当在连接的 Android 手机上运行应用程序时 我们在 Eclipse logcat 中看到大量类似这样的错误 0 GetPTLAFormat inva

随机推荐

  • 转义注释标签内的 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 加载到地图上 自定