Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用搜索栏在地图上筛选PIN及其值_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# 使用搜索栏在地图上筛选PIN及其值

C# 使用搜索栏在地图上筛选PIN及其值,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我试图根据你在搜索栏中写的内容过滤地图上的PIN。因此,如果你写下例如:“Unit”,那么只有地址为“Unit…”(ed states)”的管脚才会出现。我已经开始了一些代码,但我不知道我应该如何继续。当我现在输入东西时,每个pin都会加载,而不是与搜索栏上输入的文本匹配的pin。我想在添加管脚之前,我必须使用filterfunction 这是我的起始页: public StartPage () { searchBar.TextChanged += (sender2, e2) =>

我试图根据你在搜索栏中写的内容过滤地图上的PIN。因此,如果你写下例如:“Unit”,那么只有地址为“Unit…”(ed states)”的管脚才会出现。我已经开始了一些代码,但我不知道我应该如何继续。当我现在输入东西时,每个pin都会加载,而不是与搜索栏上输入的文本匹配的pin。我想在添加管脚之前,我必须使用filterfunction

这是我的起始页:

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 ());


                    }
                }
            }
        }

    }
private异步void FilterContacts(字符串过滤器)
{
map.Pins.Clear();
if(string.IsNullOrWhiteSpace(过滤器)){
}否则{
var getItems=wait phpApi.getInfo();
foreach(getItems[“结果”]中的var currentItem){
theName=currentItem[“Name”].ToString();//theName=string
TheAddress=currentItem[“Address”].ToString();//TheAddress=String
var theUserPosition=theaddress;
Geocoder gc=新的Geocoder();
任务结果=
gc.getPositionsforadressAsync(用户位置);
如果(TheAddress!=null){
IEnumerable data=等待结果;
foreach(数据中的位置p){
var引脚=新引脚();
销位置=新位置(p纬度,p经度);
pin.Label=名称;
pin.Address=地址;
map.Pins.Add(pin);
theaddress.ToLower().包含(filter.ToLower());
}
}
}
}
}

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

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);
    }
  }
}
公共起始页()
{
searchBar.TextChanged+=(sender2,e2)=>FilterPins(searchBar.Text);
searchBar.SearchButtonPressed+=(sender2,e2)=>FilterPins(searchBar.Text);
}
//创建一个列表来存储我们的PIN
List myPins=new List();
私有异步重写void OnAppearing(){
如果(myPins.Count==0){
myPins=等待加载数据();
filterPins(string.Empty);
}
}
//加载数据、地理编码、存储结果
专用异步列表加载数据(){
var pins=新列表();
var getItems=wait phpApi.getInfo();
foreach(getItems[“结果”]中的var currentItem){
Geocoder gc=新的Geocoder();
var pos=wait gc.getpositionsforadressasync(用户位置);
foreach(位置p中的位置){
var引脚=新引脚();
销位置=新位置(p纬度,p经度);
pin.Label=名称;
pin.Address=地址;
pins.Add(pin);
}     
}
回位销;
}
专用异步void FilterPins(字符串筛选器)
{
map.Pins.Clear();
foreach(myPins中的插脚p){
if(string.IsNullOrWhiteSpace(filter)| |(p.Address.Contains(filter)){
图.销.加(p);
}
}
}

每次用户键入你调用的FilterContacts时,它会从你的Web服务加载数据,然后对每个点进行地理编码。你真的应该只执行一次,并存储生成的数据。此外,你应该在过滤之前清除地图中的现有pin。是的,我现在一开始就清除了pin。我不太清楚你的意思。我是不是现在不这样做吗?当我开始键入filtercontacts时,它会启动。举个例子会非常感谢。TextChanged每次用户键入时都会触发-您正在执行一个昂贵的网络请求,并且每次用户键入时都会执行多个地理编码操作。这是浪费。相反,在页面加载时执行一次。然后当用户键入时,您将我只需搜索已经加载到内存中的数据。因此,当DB到达页面时,我会立即加载DB(当然不会添加管脚)。通过包含DB信息的字符串,我将它们添加到filtercontacts函数中,并添加以下内容:“TheAddress.ToLower().Contains(filter.ToLower());”//衣服是我的绳子。我理解对了吗?