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

时间:2014-10-13 14:35:54

标签: c# google-maps xamarin.ios xamarin google-maps-sdk-ios

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

为什么会这样?

2 个答案:

答案 0 :(得分: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 :(得分:1)

在运行您的示例时,您进行了阻止呼叫。

\Controller\Maps\MapControler.cs

更改: -

public override void LoadView()

以: -

public async override void LoadView()

以及更改: -

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

以: -

await StageTheView();

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

如果您仍然遇到任何困难,请告诉我您是否希望我退回解决方案?