[英]How to add markers to map from rest-api?
我在Xamarin.iOS項目中使用Google Maps組件。 我從請求中獲取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);
}
}
}
為什么會這樣呢?
從您對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的引用
運行示例時,您有一個阻止呼叫。
在您的\\Controller\\Maps\\MapControler.cs
更改:-
public override void LoadView()
至:-
public async override void LoadView()
並更改:-
Task.Run(async() => await StageTheView());
至:-
await StageTheView();
然后它將運行,並顯示您的標記。
如果您仍然遇到任何困難,請告訴我是否要我發送解決方案?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.