从db获取每个行的GPS坐标并将其指向地图上

时间:2017-02-21 07:40:09

标签: c# sql google-maps xamarin gps

修改 我已阅读Here关于如何做我需要的内容,因此我已将DbFetcher代码添加到Android项目的MainActvity.cs中

private String feedId;
private String videoUrl;
private String viewers;
private boolean is30mincomplete = true;

而且在我的PCL项目中,我已将代码更改为此处发布的原始代码,以实现Android项目中的DbFetcher内容。

我遇到的问题是让foreach方法从android项目的主要活动调用数组“allRecords”。

我需要foreach做的是得到所有Col0(ID),并且每个必须返回,Col1(名称),Col2(Lat),Col3(长)但我无法得到foreach调用android项目使用数组。

如何让它发挥作用?

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

using Xamarin.Forms.Platform.Android;
using Android.Content.PM;

[assembly: Dependency(typeof(DbFetcher))]
namespace WorkingWithMaps.Android
{

[Activity(Label = "WorkingWithMaps.Android.Android", Icon = "@drawable/icon", MainLauncher = true,
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity :
global::Xamarin.Forms.Platform.Android.FormsApplicationActivity // superclass new in 1.3
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        global::Xamarin.FormsMaps.Init(this, bundle);

        LoadApplication(new App()); // method is new in 1.3
    }
}

    class DbFetcher : IDbDataFetcher
    {

        public List<string> GetData(string conn)
        {
        conn = @"Server=<sql8.freemysqlhosting.net>;Database=<sql8159905>;User Id=<sql8159905>;Password=<FpqWnwwpjs>;Trusted_Connection=true";

        using (SqlConnection connection = new SqlConnection(conn))
            {

                SqlCommand command = new SqlCommand("select Name, Latitude, Longitude from tblTest", connection);
                try
                {
                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    var list = new List<PinPage>();
                    while (reader.Read())
                    list.Add(new PinPage {Col0 = reader.GetInt(0), Col1 = reader.GetString(1), Col2 = reader.GetDouble(2), Col3 = reader.getDouble(3)});
                    allRecords = list.ToArray();
                }
                reader.Close();
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                }
            }
            return allRecords;
        }
    }
}

0 个答案:

没有答案
相关问题