如何从rest api向映射添加标记
本文关键字:映射 添加 加标记 api rest | 更新日期: 2023-09-27 18:26:30
我在Xamarin.iOS项目中使用谷歌地图组件。我从请求中获取json,对其进行解析,并尝试在响应中添加每个数组作为mapView
上的标记。当我构建应用程序时,我不会收到错误消息,应用程序会正常运行。但是我的地图上没有标记。
MapController.cs
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Net;
using System.Json;
using System.IO;
using Google.Maps;
using MonoTouch.CoreLocation;
namespace News
{
public partial class MapController : UIViewController
{
MapView mapView;
public MapController () : base ("MapController", null)
{
Title = "Karta";
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.NavigationController.NavigationBar.TintColor = UIColor.White;
this.NavigationController.NavigationBar.SetTitleTextAttributes (new UITextAttributes { TextColor = UIColor.White });
this.NavigationController.NavigationBar.BarTintColor = UIColor.Orange;
var camera = CameraPosition.FromCamera (
latitude: 0.0,
longitude: 0.0,
zoom: 6
);
mapView = MapView.FromCamera (RectangleF.Empty, camera);
try {
var request = WebRequest.Create("http://www.example.com");
var response = request.GetResponse ();
using(var stream = new StreamReader(response.GetResponseStream())){
var json = stream.ReadToEnd ();
var jsonVal = JsonValue.Parse (json);
for(var i=0; i<jsonVal["result"].Count; i++){
//CLLocationCoordinate2D coord = new CLLocationCoordinate2D();
InvokeOnMainThread ( () => {
// manipulate UI controls
var marker = new Marker () {
Title = jsonVal["result"][i]["title"],
Snippet = jsonVal["result"][i]["address"],
Position = new CLLocationCoordinate2D (jsonVal["result"][i]["lat"],jsonVal["result"][i]["lon"])
};
marker.Map = mapView;
});
}
};
response.Close ();
}
catch
{
//
}
mapView.StartRendering ();
View = mapView;
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
}
public override void ViewWillDisappear (bool animated)
{
mapView.StopRendering ();
base.ViewWillDisappear (animated);
}
}
}
为什么会这样?
运行示例时,您有一个阻塞调用。
在您的'Controller'Maps'MapControler.cs
中
更改:-
public override void LoadView()
到:-
public async override void LoadView()
以及更改:-
Task.Run(async() => await StageTheView());
到:-
await StageTheView();
然后它将运行,并显示您的标记。
如果您仍然遇到任何困难,请告诉我是否希望我发回解决方案?
根据您对api的评论,我能够加载数据;试试这样的东西。
public override void LoadView ()
{
base.LoadView ();
CameraPosition camera = CameraPosition.FromCamera (62.3909145, 17.3098496, 15);
mapView = MapView.FromCamera (RectangleF.Empty, camera);
mapView.MyLocationEnabled = true;
Task.Run(async () => await StageTheView());
View = mapView;
}
private async Task StageTheView()
{
using (var client = new HttpClient())
{
var result = await client.GetAsync("http://www.unikabutiker.nu/api/?function=searchByName&key=kT6zAOpNk21f9UhhNWzVrK8fHjvl22K2imF1aRkvN9aScUOK6v&name=Sundsvall");
var s = "";
using (var reader = new StreamReader(await result.Content.ReadAsStreamAsync()))
{
s = await reader.ReadToEndAsync();
}
var jsonVal = JsonValue.Parse(s);
for (var i = 0; i < jsonVal["result"].Count; i++)
{
// manipulate UI controls
var marker = new Marker()
{
Title = jsonVal["result"][i]["title"],
Snippet = jsonVal["result"][i]["adress"],
Position = new CLLocationCoordinate2D(jsonVal["result"][i]["lat"], jsonVal["result"][i]["lng"])
};
marker.Map = mapView;
}
}
}
您必须添加对System.Threading.Tasks和System.Net.Http 的引用