【问题标题】:Xamarin Forms and maps - getting coords on map tap, creating routes?Xamarin 表单和地图 - 在地图上获取坐标,创建路线?
【发布时间】:2023-03-13 17:32:01
【问题描述】:

我将最新版本的 Xamarin Forms 用于我的应用程序。这个应用程序与地图紧密集成。 Android 和 iOS 是两个受支持的平台。

现在我可以实现两件事:

  1. 当我有坐标时在任何位置居中地图
  2. 添加自定义标记(引脚)

仍然需要以下功能(按重要性排序):

  1. 获取地图点击事件的坐标
  2. 获取坐标以响应带有地址的某些搜索字符串
  3. 两点之间的路由(绘制路由线)
  4. 计算两点之间的时间/距离

我是 Xamarin 的新手,我检查了 Xamarin Forms Maps API,我发现没有办法用它来实现这 4 点。

我说的对吗? 是否有任何替代的 Xamarin Forms 包? 我可以使用适用于 Android / iOS 的原生 Xamarin 地图控件来实现这些功能中的任何一个吗?

非常感谢任何信息或示例! 搜索和浏览论坛没有帮助。我找到了一些带有 Java 示例的 Google Maps API 解决方案,但我不知道如何在 Xamarin 中实现它。由于大部分应用程序已经在 Xamarin 中实现,而且我习惯于 C#,因此我不想更改平台。

【问题讨论】:

标签: c# android ios xamarin xamarin.forms


【解决方案1】:

您需要查找原生 iOS 和 Android 实现来完成这项工作,但您应该使用 SAP 或 PCL 的依赖注入来调用原生实现以满足您的需要。

*注意 - 这是为了提供如何进行此操作的一般概述,它不是复制粘贴实现。没有实现打开地图,也没有说明如何或何时在共享代码中获取坐标。

例子

在共享代码中 -

interface IMapping
{
    public OpenMap(); 
    public Model<GeoPoint>GetLastTouchCoordinates();         
}

在 Android 项目中 -

//For use with Xamarin.Forms.DependencyService will not be necessary with other IoC containers if not using forms look up Tiny IoC, Unity or one of the other DI containers
[assembly: Xamarin.Forms.Dependency(typeof(YourNameSpace.Droid.YourClass))] 

public class GoogleMap : FragmentActivity, IMapping, IOnMapClickListener

{
 private GoogleMap mMap;
 private Point lastClickedPoint;

protected void OnCreate()
{
    base.OnCreate();
    setContentView(some map fragment layout)

    //set the map touch listener
}

private void handletouch()// implementation of IOnMapClickListener Interface
{
    //google how to get the coordinates in android on a map touch
    lastClickedPoint = new Point();
    lastClickedPoint.lat = args.lat;
    lastClickedPoint.long = args.long;
}

private Model<GeoPoint> GetLastTouchCoordinates()
{
    if(lastclickedPoint != null)
    {
    Model<Geopoint> gp = new Model<Geopoint>();
    gp.latitude = p.latitude;
    gp.longitude = p.longitude;
    return gp;
    }
    else
    {
      return Point with some indication that you have no point
    }

}
}

在共享代码中--

现在要使用它,只需编写类似这样的内容

DependencyService.Get<IMapping>.OpenMap()
DependencyService.Get<IMapping>.GetLastTouchCoordinates()

链接到有关跨平台代码中的依赖注入的更多详细信息 http://adventuresinxamarinforms.com/2014/11/17/creating-a-xamarin-forms-application-part-4-dependency-injection/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    相关资源
    最近更新 更多