使用搜索栏在地图上过滤引脚及其值

时间:2016-03-17 21:48:13

标签: c# xamarin xamarin.forms

我试图根据您在搜索栏中写的内容过滤地图上的图钉。所以,如果你写的是:" Unit"然后只有带有地址的引脚"单元....(ed states)"会出现。我已经开始使用一些代码,但我不确定应该如何进行。当我输入内容时,每个引脚加载而不是与搜索栏上输入的文本匹配的引脚。我想在添加引脚之前必须使用滤波器功能。

这是我的startPage:

public StartPage ()
{
    searchBar.TextChanged += (sender2, e2) => FilterContacts(searchBar.Text);

    searchBar.SearchButtonPressed += (sender2, e2) => FilterContacts(searchBar.Text);
}

这是过滤器发生的地方。我从我的数据库中获取数据。

private  async void FilterContacts (string filter)
    {
        map.Pins.Clear ();
        if (string.IsNullOrWhiteSpace (filter)) {

        } else {

            var getItems = await phpApi.getInfo ();

            foreach (var currentItem in getItems["results"]) {

                theName = currentItem ["Name"].ToString (); //theName = string
                theAdress = currentItem ["Adress"].ToString (); //theAdress = String

                var theUserPosition = theAdress;
                Geocoder gc = new Geocoder ();
                Task<IEnumerable<Position>> result =
                    gc.GetPositionsForAddressAsync (theUserPosition);

                if (theAdress != null) {

                    IEnumerable<Position> data = await result;

                    foreach (Position p in data) {

                        var pin = new Pin ();
                        pin.Position = new Position (p.Latitude, p.Longitude);
                        pin.Label = theName;
                        pin.Address = theAdress;                    
                        map.Pins.Add (pin);

                        theAdress.ToLower ().Contains (filter.ToLower ());


                    }
                }
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

加载数据并对其进行地理编码并存储;然后只调用过滤器逻辑

public StartPage ()
{
    searchBar.TextChanged += (sender2, e2) => FilterPins(searchBar.Text);

    searchBar.SearchButtonPressed += (sender2, e2) => FilterPins(searchBar.Text);
}

// create a list to store our Pins 
List<Pin> myPins = new List<Pin>();

private async override void OnAppearing() {

  if (myPins.Count == 0) {
    myPins = await LoadData();

    filterPins(string.Empty);
  }
}

// load the data, geocode, store results
private async List<Pin> LoadData() {

  var pins = new List<Pin>();

  var getItems = await phpApi.getInfo ();

  foreach (var currentItem in getItems["results"]) {

    Geocoder gc = new Geocoder ();
    var pos = await gc.GetPositionsForAddressAsync (theUserPosition);

    foreach (Position p in pos) {

      var pin = new Pin ();
      pin.Position = new Position (p.Latitude, p.Longitude);
      pin.Label = theName;
      pin.Address = theAdress;                    

      pins.Add(pin);
    }     
  }

  return pins;
}

private  async void FilterPins (string filter)
{
  map.Pins.Clear ();

  foreach(Pin p in myPins) {
    if (string.IsNullOrWhiteSpace(filter) || (p.Address.Contains(filter)) {
      map.Pins.Add(p);
    }
  }
}