Xamarin.Forms 调用腾讯地图
用Xamarin.Forms 也有一段时间了,跨平台,生成native代码的噱头 天花乱坠的,
其中的坑,只有用过的人才懂。。。
就拿地图来说。。。总不能用google地图吧 于是只能自己想办法了。
首先官网上有篇文章,大家可以看下
首先我们需要做的是 将地图的android sdk转为C#能用的dll ,也就是需要对android sdk做一个binding
由于百度地图及高德地图的api key申请都需要sha1值,个人嫌麻烦,所以选择了腾讯地图,腾讯地图只要api key即可。
1.先创建一个Android Bindings Library项目,命名为TencentMapBinding
2.将腾讯地图官网下载的sdk拖到Jars目录下,并将此jar文件的属性改为EmbeddedJar
3.修改Transforms目录下的Metadata.xml,将MapController类删掉
4.编译
然后再新建一个xamarin.forms项目,命名为MyTencentMap,在Portable中添加地图的页面
public class TencentMapPage : ContentPage { public static BindableProperty PinsProperty =
BindableProperty.Create<TencentMapPage, IEnumerable>(p => p.Pins, default(IEnumerable)); public IEnumerable Pins {
get {
return (IEnumerable)GetValue(PinsProperty);
}
set {
this.SetValue(PinsProperty, value);
}
}
}
再增加一个类
public class Page1:TencentMapPage { }
随便弄个实体类,显示数据用
public class UserTaskEntInfo : BaseIntInfo {
public string PsCode { get; set; }
public string BaseInfo { get; set; }
public string ProduceInfo { get; set; }
public string TreatmentInfo { get; set; }
public string DischargeInfo { get; set; }
public string Address { get; set; }
public double? Longitude { get; set; }
public double? Latitude { get; set; }
} public class BaseIntInfo : BaseInfo<int> {
} public abstract class BaseInfo<TId> { /// <summary>
/// 标识
/// </summary>
public TId Id { get; set; } /// <summary>
/// 名称
/// </summary>
public string Name { get; set; } }
App.cs文件中添加如下代码:
public App() {
// The root page of your application
var pins = new List<UserTaskEntInfo>() { }; pins.Add(new UserTaskEntInfo() { Name = "上海市浦东新区陆家嘴街道乳山二村", Longitude = 31.2317331909, Latitude = 121.5170146362 });
pins.Add(new UserTaskEntInfo() { Name = "上海市浦东新区昌邑路555弄", Longitude = 31.2431982838, Latitude = 121.5215228609 });
pins.Add(new UserTaskEntInfo() { Name = "上海市浦东新区东昌路267号", Longitude = 31.2316324310, Latitude = 121.5063730654 });
pins.Add(new UserTaskEntInfo() { Name = "上海市浦东新区滨江大道", Longitude = 31.2379863826, Latitude = 121.4959153979 });
pins.Add(new UserTaskEntInfo() { Name = "上海市浦东新区即墨路", Longitude = 31.2435242682, Latitude = 121.5104350816 }); NavigationTestPage = new NavigationPage(new Page1() { Pins = pins });
MainPage = NavigationTestPage;
}
在MyTencentMap.Droid中将第一步binding生成的dll引入进来,
添加CustomTencentMapRenderer.cs
[assembly: ExportRenderer(typeof(TencentMapPage), typeof(CustomTencentMapRenderer))]
namespace MyTencentMap.Droid {
public class CustomTencentMapRenderer : PageRenderer { private TencentMapPage myAMapPage;
private LinearLayout layout1;
private MapView mapView;
private Bundle bundle; protected override void OnElementChanged(ElementChangedEventArgs<Page> e) { base.OnElementChanged(e);
myAMapPage = e.NewElement as TencentMapPage;
layout1 = new LinearLayout(this.Context);
this.AddView(layout1); mapView = new MapView(this.Context) {
LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
};
LatLng SHANGHAI = new LatLng( 31.238068, 121.501654);
mapView.Map.SetCenter(SHANGHAI); var pins = myAMapPage.Pins; Drawable d = Resources.GetDrawable(Resource.Drawable.red_location);
Bitmap bitmap = ((BitmapDrawable)d).Bitmap;
LatLng latLng1;
foreach (UserTaskEntInfo pin in pins) {
latLng1 = new LatLng(pin.Longitude ?? 31.238068, pin.Latitude ?? 121.501654);
var markOption = new MarkerOptions();
markOption.InvokeIcon(new BitmapDescriptor(bitmap));
markOption.InvokeTitle(pin.Name);
markOption.InvokePosition(latLng1);
var fix = mapView.Map.AddMarker(markOption);
fix.ShowInfoWindow();
}
mapView.Map.SetZoom();
mapView.OnCreate(bundle);
layout1.AddView(mapView); } protected override void OnLayout(bool changed, int l, int t, int r, int b) {
base.OnLayout(changed, l, t, r, b);
var msw = View.MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
var msh = View.MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
layout1.Measure(msw, msh);
layout1.Layout(, , r - l, b - t);
}
}
}
编译即可。
最后附上截图:
本文的代码可以从https://github.com/Joetangfb/Sharper中获取。
Xamarin.Forms 调用腾讯地图的更多相关文章
- Xamarin.Forms 调用 腾讯地图SDK
Xamarin.Forms研究了好一段时间了,最近一直在学习中,想尝试一下调用其他的SDK,就如腾讯地图SDK(申请容易). 完成此次项目得感谢以下链接: http://www.cnblogs.com ...
- 微信小程序wx.getLocation()获取经纬度及JavaScript SDK调用腾讯地图API获取某一类地址
简介 腾讯位置服务为微信小程序提供了基础的标点能力.线和圆的绘制接口等地图组件和位置展示.地图选点等地图API位置服务能力支持,使得开发者可以自由地实现自己的微信小程序产品. 在此基础上,腾讯位置服务 ...
- H5调用腾讯地图
获取当前定位的经纬度并在容器内显示当前位置 (安卓上的位置有点偏差) 在vue的index.html中需要引用 template <div id="container" st ...
- C# 调用腾讯地图WebService API获取距离(一对多)
官方文档地址:https://lbs.qq.com/webservice_v1/guide-distance.html 代码: /// <summary> /// 获取距离最近的点的经纬度 ...
- 微信小程序 - 调用腾讯地图插件
1. 登录公众号平台 https://mp.weixin.qq.com/ 2. 设置->第三方服务->添加插件->输入插件名称->申请 3. 项目中使用 3.1 app.jso ...
- Xamarin.Forms介绍
On May 28, 2014, Xamarin introduced Xamarin.Forms, which allows you to write user-interface code tha ...
- [微信小程序] 微信小程序获取用户定位信息并加载对应城市信息,wx.getLocation,腾讯地图小程序api,微信小程序经纬度逆解析地理信息
因为需要在小程序加个定位并加载对应城市信息 然而小程序自带api目前只能获取经纬度不能逆解析,虽然自己解析方式,但是同时也要调用地图,难道用户每次进小程序还要强行打开地图选择地址才定位吗?多麻烦也不利 ...
- 百度地图转腾讯地图腾讯地图转百度地图(还有方法二就是使用百度地图api 转火星坐标)
public static double pi = 3.141592653589793 * 3000.0 / 180.0; /** * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转 ...
- 腾讯地图 API 调用入门
本文仅为腾讯地图 API 调用入门,如需进阶学习,请在腾讯位置服务网站上进行学习. 登陆网址 https://lbs.qq.com/ 点击右上角的登陆按钮,需要进行注册按照流程进行就好. 完成之后,选 ...
随机推荐
- vb如何将数据库中某个字段显示在一个文本框
Dim mrc As ADODB.Recordset Private Sub cmdQuery_Click() Dim txtSQL As String Dim MsgText As String t ...
- 【bzoj4894】天赋 矩阵树定理
题目描述 小明有许多潜在的天赋,他希望学习这些天赋来变得更强.正如许多游戏中一样,小明也有n种潜在的天赋,但有一些天赋必须是要有前置天赋才能够学习得到的.也就是说,有一些天赋必须是要在学习了另一个天赋 ...
- Hash表模板
namespace Hash { ; ; struct adj { ll nxt,v,num,val; }e[N]; ll head[H],ecnt=; void init() { ecnt=; me ...
- HTML,CSS,font-family:中文字体的英文名称【转载】
转自:http://www.9958.pw/post/html_font-family 宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft ...
- 【马克-to-win】—— 学习笔记
声明 以下学习内容转载自:http://www.mark-to-win.com/ 社区,由马克java社区创始人---"马克-to-win"一人全部独立写作,创作和制作. 非常感谢 ...
- phantomjs和selenium模拟登陆qq空间
# -*- coding: utf-8 -*- from selenium import webdriver import time driver =webdriver.PhantomJS() dri ...
- WINDOWS2008 KMS 服务器安装及激活
搭建环境条件: windows server 2008 enterprise 安装光盘kms密钥kms服务安装步骤: 安装第一台windows server 2008 enterprise服务器用km ...
- Linux Mint---shutter截图软件
shutter 可以说是linux下最好的截图软件了,默认安装好后不能编辑截图,解决方法如下: 1-关闭shutter 2-sudo apt-get install shutter libgoo-ca ...
- JS与jQuery中html-与-text方法的区别
所有的实例均用下面的html <div id="id0"> <div id="id1"> 直接text <p> <sp ...
- sqlalchemy多表查询
from datetime import datetime from sqlalchemy import Column,Integer,String,Boolean,DateTime,ForeignK ...