如何添加标记以从rest-api映射?

问题描述 投票:1回答:2

我在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);
                }
        }
}

为什么会这样呢?

c# google-maps xamarin.ios xamarin google-maps-sdk-ios
2个回答
1
投票

从您对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的引用


1
投票

运行示例时,您有一个阻止呼叫。

在您的\\Controller\\Maps\\MapControler.cs

更改:-

public override void LoadView()

至:-

public async override void LoadView()

更改:-

Task.Run(async() => await StageTheView());

至:-

await StageTheView();

然后它将运行,并显示您的标记。

如果您仍然遇到任何困难,请告诉我是否要我发送解决方案?

© www.soinside.com 2019 - 2024. All rights reserved.