使用搜索栏在地图上过滤图钉及其值
Filter pins and its value on a map with a searchbar
我正在尝试根据您在搜索栏中所写的内容来过滤地图上的图钉。因此,例如,如果您这样写:"Unit",那么只会显示地址为 "Unit....(ed states)" 的引脚。我从一些代码开始,但我不确定我应该如何继续。现在,当我输入内容时,每个图钉都会加载,而不是与搜索栏上输入的文本匹配的图钉。我想我必须在添加引脚之前使用过滤器功能。
这是我的起始页:
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 ());
}
}
}
}
}
加载数据并对其进行一次地理编码并存储;然后只调用过滤逻辑
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);
}
}
}
我正在尝试根据您在搜索栏中所写的内容来过滤地图上的图钉。因此,例如,如果您这样写:"Unit",那么只会显示地址为 "Unit....(ed states)" 的引脚。我从一些代码开始,但我不确定我应该如何继续。现在,当我输入内容时,每个图钉都会加载,而不是与搜索栏上输入的文本匹配的图钉。我想我必须在添加引脚之前使用过滤器功能。
这是我的起始页:
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 ());
}
}
}
}
}
加载数据并对其进行一次地理编码并存储;然后只调用过滤逻辑
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);
}
}
}