Xamarin表格和地图-在地图水龙头上获取坐标,创建路线? [英] Xamarin Forms and maps - getting coords on map tap, creating routes?

查看:80
本文介绍了Xamarin表格和地图-在地图水龙头上获取坐标,创建路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序使用了最新版本的Xamarin Forms.这个应用程式与地图紧密整合. Android和iOS是两个受支持的平台.

I use the latest version of Xamarin Forms for my application. This app has a tense integration with maps. Android and iOS are two supported platforms.

现在我可以实现两件事:

Right now I'm able to implement two things:

  1. 在有坐标的情况下将地图定位在任何位置上
  2. 添加自定义标记(图钉)

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

The following functions are still needed (in order of importance):

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

我是Xamarin的新手,我检查了Xamarin Forms Maps API,并且我发现无法用它实现这4点.

I'm new to Xamarin, I checked the Xamarin Forms Maps API and as I see there's no way to implement these 4 points with it.

我是对的吗? 是否有其他替代Xamarin Forms软件包? 我可以使用适用于Android/iOS的本地Xamarin地图控件来实现这些功能吗?

Am I right? Is there any alternative Xamarin Forms packages for it? Can I implement any of these features using native Xamarin map controls for Android / iOS?

任何信息或示例都将受到高度赞赏! 搜索和浏览论坛无济于事.我找到了一些带有Java示例的Google Maps API解决方案,但是我不知道如何在Xamarin中实现它.由于应用程序的很大一部分已经在Xamarin中实现,而且我已经习惯了C#,所以我不想更改平台.

Any info or examples are highly appreciated! Searching and browsing forums didn't help. I found some solutions for Google Maps API with examples in Java, but I have no idea how to implement it in Xamarin. Since a big part of application is already implemented in Xamarin and I'm used to C# I don't want to change the platform.

推荐答案

您将需要查找本机iOS和Android实现来进行工作,但是您应该使用来自SAP或PCL的依赖项注入来调用本机实现满足您的需求.

You will need to look up the native iOS and Android implementations to do the work but you should use dependency injection from your SAP or PCL to call to the native implementations for what you need.

*注意-这是一个概述,概述了它不是复制粘贴的实现方法.未实现开放地图,也未说明如何或何时获取共享代码中的坐标.

*Note - this is to provide a general outline of how to go about this it is not a copy paste implementation. Open map is not implemented nor are the instructions of how or when to grab the coordinates in the shared code.

示例

使用共享代码-

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

在Android项目中-

In Android project -

//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
    }

}
}

使用共享代码-

现在要使用它,只需编写如下内容

Now to use it simply write something like this

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

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

Link to more details about Dependency Injection in cross platform code http://adventuresinxamarinforms.com/2014/11/17/creating-a-xamarin-forms-application-part-4-dependency-injection/

这篇关于Xamarin表格和地图-在地图水龙头上获取坐标,创建路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆