Exemplo n.º 1
0
        public void EmptyRequest()
        {
            if (!TestRuntime.CheckSystemAndSDKVersion(6, 1))
            {
                Assert.Inconclusive("Requires iOS 6.1");
            }

            using (var lsr = new MKLocalSearchRequest())
                using (MKLocalSearch ls = new MKLocalSearch(lsr)) {
                    lsr.Region = new MKCoordinateRegion(new CLLocationCoordinate2D(47, -71), new MKCoordinateSpan(1, 1));
                    bool wait = true;
                    ls.Start((MKLocalSearchResponse response, NSError error) => {
                        wait = false;
                    });
                    Assert.True(ls.IsSearching, "IsSearching");

                    // wait a bit before cancelling the search (so it really starts)
                    // otherwise IsSearching might never complete (on iOS8) and seems very random (in earlier versions)
                    NSRunLoop.Main.RunUntil(NSDate.Now.AddSeconds(2));
                    ls.Cancel();

#if false
                    // give it some time to cancel - but eventually time out
                    int counter = 0;
                    while (wait && (counter < 5))
                    {
                        NSRunLoop.Main.RunUntil(DateTime.Now.AddSeconds(counter));
                        counter++;
                    }

                    Assert.False(ls.IsSearching, "IsSearching/Cancel");
#endif
                }
        }
            private void LoadLocations()
            {
                if (_coords == null)
                {
                    return;
                }

                var coordinate = new CLLocationCoordinate2D(_coords.Value.Latitude, _coords.Value.Longitude);

                var localSearch = new MKLocalSearch(new MKLocalSearchRequest()
                {
                    NaturalLanguageQuery = "coffee",
                    Region = new MKCoordinateRegion(coordinate, new MKCoordinateSpan(1.25, 1.25))
                });

                localSearch.Start((response, error) => {
                    if (response != null && error == null)
                    {
                        _locations = response.MapItems;
                        _parent.OnLocationsUpdate(_locations);
                    }
                    else
                    {
                        Console.WriteLine("local search error: {0}", error);
                    }
                });
            }
Exemplo n.º 3
0
        public void Search(string forSearchString)
        {
            // create search request
            var searchRequest = new MKLocalSearchRequest();

            searchRequest.NaturalLanguageQuery = forSearchString;

            //searchRequest.Region = new MKCoordinateRegion(map.UserLocation.Coordinate, new MKCoordinateSpan(0.75, 0.75));
            const double lat       = 43.161030;
            const double lon       = -77.610924;
            var          mapCenter = new CLLocationCoordinate2D(lat, lon);

            searchRequest.Region = new MKCoordinateRegion(mapCenter, new MKCoordinateSpan(0.75, 0.75));

            // perform search
            var localSearch = new MKLocalSearch(searchRequest);

            localSearch.Start(delegate(MKLocalSearchResponse response, NSError error)
            {
                if (response != null && error == null)
                {
                    this.MapItems = response.MapItems.ToList();
                    this.TableView.ReloadData();
                }
                else
                {
                    Console.WriteLine("local search error: {0}", error);
                }
            });
        }
        // TODO: Step 5c - Search for items of interest in the current map view
        private void SearchForLocationsNearPosition(MKMapView mapView)
        {
            var searchRequest = new MKLocalSearchRequest();

            searchRequest.NaturalLanguageQuery = "Bakery";
            searchRequest.Region = mapView.Region;

            var localSearch = new MKLocalSearch(searchRequest);

            localSearch.Start(delegate(MKLocalSearchResponse response, NSError error)
            {
                if (response != null && error == null)
                {
                    var items = response.MapItems.ToList();
                    PlotItems(mapView, items);

                    // zoom in on the first item we plotted
                    var firstItem = items.FirstOrDefault();
                    if (firstItem != null)
                    {
                        MKCoordinateSpan span        = new MKCoordinateSpan(0.8, 0.8);
                        CLLocationCoordinate2D coord = new CLLocationCoordinate2D(
                            firstItem.Placemark.Location.Coordinate.Latitude,
                            firstItem.Placemark.Location.Coordinate.Longitude);
                        mapView.Region = new MKCoordinateRegion(coord, span);
                    }
                }
                else
                {
                    Console.WriteLine("local search error: {0}", error);
                }
            });
        }
Exemplo n.º 5
0
        public async Task <IEnumerable <MKMapItem> > SearchAsync(
            string query,
            bool aroundRegion,
            double centerLatitude,
            double centerLongitude,
            double deltaLatitude  = 0.25,
            double deltaLongitude = 0.25)
        {
            var request = new MKLocalSearchRequest();

            request.NaturalLanguageQuery = query;

            if (aroundRegion)
            {
                var center = new CLLocationCoordinate2D(centerLatitude, centerLongitude);
                var span   = new MKCoordinateSpan(deltaLatitude, deltaLongitude);
                request.Region = new MKCoordinateRegion(center, span);
            }

            var search   = new MKLocalSearch(request);
            var response = await search.StartAsync();

            if (response == null)
            {
                return(null);
            }
            if (response.MapItems == null)
            {
                return(null);
            }

            return(response.MapItems);
        }
Exemplo n.º 6
0
        void search(object sender, EventArgs e)
        {
            var searcRequest = new MKLocalSearchRequest();

            searcRequest.NaturalLanguageQuery = searchBar.Text;
            searcRequest.Region = new MKCoordinateRegion(map.UserLocation.Coordinate, new MKCoordinateSpan(0.25, 0.25));
            var activeSearch = new MKLocalSearch(request: searcRequest);

            activeSearch.Start(delegate(MKLocalSearchResponse response, NSError error) {
                if (response != null && error == null)
                {
                    double latitude       = response.Region.Center.Latitude;
                    double longitude      = response.Region.Center.Longitude;
                    var annotation        = new MKPointAnnotation();
                    annotation.Title      = searchBar.Text;
                    annotation.Coordinate = new CLLocationCoordinate2D(latitude, longitude);
                    map.AddAnnotation(annotation);
                    map.SetRegion(response.Region, animated: true);
                }
                else
                {
                    Console.WriteLine("ERROR");
                }
            });
        }
        // Performs a natural language search for locations in the map's region that match the `searchBar`'s text.
        void PerformSearch()
        {
            var request = new MKLocalSearchRequest();

            request.NaturalLanguageQuery = SearchBar.Text;
            const double multiplier = RegionQueryDegreeMultiplier;
            var          querySpan  = new MKCoordinateSpan(MapView.Region.Span.LatitudeDelta * multiplier, MapView.Region.Span.LongitudeDelta * multiplier);

            request.Region = new MKCoordinateRegion(MapView.Region.Center, querySpan);

            var search        = new MKLocalSearch(request);
            var matchingItems = new List <MKMapItem> ();

            search.Start((response, error) => {
                MKMapItem[] mapItems = null;
                if (response != null)
                {
                    mapItems = response.MapItems ?? new MKMapItem[0];
                }

                foreach (var item in mapItems)
                {
                    matchingItems.Add(item);
                    var annotation        = new MKPointAnnotation();
                    annotation.Coordinate = item.Placemark.Coordinate;
                    annotation.Title      = item.Name;
                    MapView.AddAnnotation(annotation);
                }
            });
        }
Exemplo n.º 8
0
        private async Task SearchAsync(string what, string where)
        {
            var span    = new MKCoordinateSpan(0.25, 0.25);
            var request = new MKLocalSearchRequest
            {
                NaturalLanguageQuery = what,
                Region = new MKCoordinateRegion(ParseCoordinates(where), span),
            };

            var search = new MKLocalSearch(request);
            MKLocalSearchResponse response = null;

            try
            {
                response = await search.StartAsync();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }

            if (response != null)
            {
                searchItems = response.MapItems;
                PerformSegue(SearchSegueIdentifier, this);
            }
        }
Exemplo n.º 9
0
		/// <summary>
		/// Finds the nearest restaurant.
		/// </summary>
		/// <returns>The nearest restaurant.</returns>
		/// <param name="location">Location.</param>
		public async static Task<string> FindNearestRestaurantAsync(CLLocation location)
		{
			// Search for restaurants near our location.
			string restaurant = string.Empty;
			try
			{
				var searchRequest = new MKLocalSearchRequest
				{
					NaturalLanguageQuery = "food",
					Region = new MKCoordinateRegion(new CLLocationCoordinate2D(location.Coordinate.Latitude, location.Coordinate.Longitude), new MKCoordinateSpan(0.3f, 0.3f))
				};


				var localSearch = new MKLocalSearch(searchRequest);
				var localSearchResponse = await localSearch.StartAsync();
				if(localSearchResponse.MapItems != null && localSearchResponse.MapItems.Length > 0)
				{
					var mapItem = localSearchResponse.MapItems[0];
					restaurant = mapItem.Name;
				}
			}
			catch(Exception ex)
			{
				//Console.WriteLine("Error searching restaurants: " + ex);
			}
			return restaurant;
		}
        public void UpdateSearchResults(string query)
        {
            if (!string.IsNullOrEmpty(query))
            {
                // create search request
                var searchRequest = new MKLocalSearchRequest
                {
                    NaturalLanguageQuery = query,
                    Region = new MKCoordinateRegion(map.UserLocation.Coordinate, new MKCoordinateSpan(0.25, 0.25))
                };

                // perform search
                var localSearch = new MKLocalSearch(searchRequest);
                localSearch.Start((response, error) =>
                {
                    if (response != null && error == null)
                    {
                        items = new List <MKMapItem>(response.MapItems);
                        this.TableView.ReloadData();
                    }
                    else
                    {
                        Console.WriteLine($"local search error: {error?.LocalizedDescription ?? ""}");
                    }
                });
            }
        }
Exemplo n.º 11
0
        ///<inheritdoc/>
        public async Task <IEnumerable <IPlaceResult> > GetPredictions(string query, MapSpan bounds)
        {
            List <IPlaceResult> result = new List <IPlaceResult>();

            var region = new MKCoordinateRegion(bounds.Center.ToLocationCoordinate(), new MKCoordinateSpan(0.25, 0.25));

            var request = new MKLocalSearchRequest
            {
                NaturalLanguageQuery = query,
                Region = region
            };

            MKLocalSearch search       = new MKLocalSearch(request);
            var           nativeResult = await search.StartAsync();

            if (nativeResult != null && nativeResult.MapItems != null)
            {
                result.AddRange(nativeResult.MapItems.Select(i =>
                                                             new TKNativeiOSPlaceResult
                {
                    Description = string.Format("{0}, {1} {2}", i.Placemark.Title, i.Placemark.AdministrativeArea, i.Placemark.SubAdministrativeArea),
                    Details     = new TKPlaceDetails
                    {
                        Coordinate               = i.Placemark.Coordinate.ToPosition(),
                        FormattedAddress         = i.Placemark.Title,
                        InternationalPhoneNumber = i.PhoneNumber.ToString(),
                        Website = i.Url.ToString()
                    }
                }));
                return(result);
            }
            return(null);
        }
Exemplo n.º 12
0
        ///<inheritdoc/>
        public async Task<IEnumerable<IPlaceResult>> GetPredictions(string query, MapSpan bounds)
        {
            List<IPlaceResult> result = new List<IPlaceResult>();

            var region = new MKCoordinateRegion(bounds.Center.ToLocationCoordinate(), new MKCoordinateSpan(0.25, 0.25));

            var request = new MKLocalSearchRequest 
            {
                NaturalLanguageQuery = query,
                Region = region
            };

            MKLocalSearch search = new MKLocalSearch(request);
            var nativeResult = await search.StartAsync();

            if (nativeResult != null && nativeResult.MapItems != null)
            {
                result.AddRange(nativeResult.MapItems.Select(i =>
                    new TKNativeiOSPlaceResult
                    {
                        Description =  string.Format("{0}, {1} {2}", i.Placemark.Title, i.Placemark.AdministrativeArea, i.Placemark.SubAdministrativeArea),
                        Details = new TKPlaceDetails 
                        {
                            Coordinate = i.Placemark.Coordinate.ToPosition()
                        }
                    }));
                return result;
            }
            return null;
        }
Exemplo n.º 13
0
		async Task SearchAsync (string what, string where)
		{
			var coord = here == null ? Parse (where) : here.Coordinate;

			MKCoordinateSpan span = new MKCoordinateSpan (0.25, 0.25);
			MKLocalSearchRequest request = new MKLocalSearchRequest ();
			request.Region = new MKCoordinateRegion (coord, span);
			request.NaturalLanguageQuery = what;
			MKLocalSearch search = new MKLocalSearch (request);
			MKLocalSearchResponse response;
			try{
				response = await search.StartAsync ();

			}
			catch(Exception e){
				return;
			}	
			if (response == null)			
				return;	

			var section = new Section ("Search Results for " + what);
			results.Clear ();
			foreach (MKMapItem mi in response.MapItems) {
				results.Add (mi);
				var element = new StyledStringElement (mi.Name, mi.PhoneNumber, UITableViewCellStyle.Subtitle);
				element.Accessory = UITableViewCellAccessory.DisclosureIndicator;
				element.Tapped += () => { results [element.IndexPath.Row].OpenInMaps (); };
				section.Add (element);
			}

			var root = new RootElement ("MapKit Search Sample") { section };
			var dvc = new DialogViewController (root);
			(window.RootViewController as UINavigationController).PushViewController (dvc, true);

		}
Exemplo n.º 14
0
        public async Task SearchAsync()
        {
            var request = new MKLocalSearchRequest();

            request.NaturalLanguageQuery = "Grocery stores";
            request.Region = map.Region;            //we'll search on the current screen

            var local = new MKLocalSearch(request);

            var response = await local.StartAsync();

            if (response != null && response.MapItems.Length > 0)
            {
                var stores = new List <GroceryStore> ();

                foreach (var item in response.MapItems)
                {
                    stores.Add(new GroceryStore()
                    {
                        Name      = item.Name, PhoneNumber = item.PhoneNumber, Address = item.Placemark.Title,
                        Longitude = item.Placemark.Location.Coordinate.Longitude, Latitude = item.Placemark.Location.Coordinate.Latitude
                    });
                }
                AddStoreAnnotations(stores);
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Finds the nearest restaurant.
        /// </summary>
        /// <returns>The nearest restaurant.</returns>
        /// <param name="location">Location.</param>
        public async static Task <string> FindNearestRestaurantAsync(CLLocation location)
        {
            // Search for restaurants near our location.
            string restaurant = string.Empty;

            try
            {
                var searchRequest = new MKLocalSearchRequest
                {
                    NaturalLanguageQuery = "food",
                    Region = new MKCoordinateRegion(new CLLocationCoordinate2D(location.Coordinate.Latitude, location.Coordinate.Longitude), new MKCoordinateSpan(0.3f, 0.3f))
                };


                var localSearch         = new MKLocalSearch(searchRequest);
                var localSearchResponse = await localSearch.StartAsync();

                if (localSearchResponse.MapItems != null && localSearchResponse.MapItems.Length > 0)
                {
                    var mapItem = localSearchResponse.MapItems[0];
                    restaurant = mapItem.Name;
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine("Error searching restaurants: " + ex);
            }
            return(restaurant);
        }
Exemplo n.º 16
0
        async Task SearchAsync(string what, string where)
        {
            var coord = here == null?Parse(where) : here.Coordinate;

            MKCoordinateSpan     span    = new MKCoordinateSpan(0.25, 0.25);
            MKLocalSearchRequest request = new MKLocalSearchRequest();

            request.Region = new MKCoordinateRegion(coord, span);
            request.NaturalLanguageQuery = what;
            MKLocalSearch         search = new MKLocalSearch(request);
            MKLocalSearchResponse response;

            try{
                response = await search.StartAsync();
            }
            catch {
                return;
            }
            if (response == null)
            {
                return;
            }

            var section = new Section("Search Results for " + what);

            results.Clear();
            foreach (MKMapItem mi in response.MapItems)
            {
                results.Add(mi);
                var element = new StyledStringElement(mi.Name, mi.PhoneNumber, UITableViewCellStyle.Subtitle);
                element.Accessory = UITableViewCellAccessory.DisclosureIndicator;
                element.Tapped   += () => { results [element.IndexPath.Row].OpenInMaps(); };
                section.Add(element);
            }

            var root = new RootElement("MapKit Search Sample")
            {
                section
            };
            var dvc = new DialogViewController(root);

            (window.RootViewController as UINavigationController).PushViewController(dvc, true);
        }
Exemplo n.º 17
0
        private Task <MKPlacemark[]> SearchAsync(string query, double?lat, double?lng, double radiusInMeters)
        {
            var tcs    = new TaskCompletionSource <MKPlacemark[]>();
            var result = new MKPlacemark[0];

            var o = new NSObject();

            o.InvokeOnMainThread(async() =>
            {
                try
                {
                    var searchRequest = new MKLocalSearchRequest {
                        NaturalLanguageQuery = query
                    };

                    if (lat.HasValue && lng.HasValue &&
                        lat.Value != 0 && lng.Value != 0)
                    {
                        // You can use this parameter to narrow the list of search results to those inside or close to the specified region.
                        // Specifying a region does not guarantee that the results will all be inside the region. It is merely a hint to the search engine.

                        var region           = MKCoordinateRegion.FromDistance(new CLLocationCoordinate2D(lat.Value, lng.Value), radiusInMeters * 2, radiusInMeters * 2);
                        searchRequest.Region = region;
                    }

                    var search = new MKLocalSearch(searchRequest);

                    var searchResult = await search.StartAsync();
                    result           = searchResult.MapItems.Select(x => x.Placemark).ToArray();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    tcs.TrySetResult(result);
                }
            });

            return(tcs.Task);
        }
		public void Search (string forSearchString)
		{
			// create search request
			var searchRequest = new MKLocalSearchRequest ();
			searchRequest.NaturalLanguageQuery = forSearchString;
			searchRequest.Region = new MKCoordinateRegion (map.UserLocation.Coordinate, new MKCoordinateSpan (0.25, 0.25));

			// perform search
			var localSearch = new MKLocalSearch (searchRequest);

			localSearch.Start (delegate (MKLocalSearchResponse response, NSError error) {
				if (response != null && error == null) {
					this.MapItems = response.MapItems.ToList ();
					this.TableView.ReloadData ();
				} else {
					Console.WriteLine ("local search error: {0}", error);
				}
			});


		}
        // TODO: Step 5a - search for an address
        private void SearchForAddress(MKMapView mapView)
        {
            var searchRequest = new MKLocalSearchRequest ();
            searchRequest.NaturalLanguageQuery = "Main Street, New York, NY";
            // searchRequest.Region = new MKCoordinateRegion (mapView.UserLocation.Coordinate, new MKCoordinateSpan (0.25, 0.25));

            // perform search
            var localSearch = new MKLocalSearch (searchRequest);
            localSearch.Start (delegate (MKLocalSearchResponse response, NSError error) 
            {
                if (response != null && error == null) 
                {
                    var items = response.MapItems.ToList();
                    PlotItems(mapView, items);
                } 
                else 
                {
                    Console.WriteLine ("local search error: {0}", error);
                }
            });                
        }
        public void Search(string forSearchString)
        {
            var searchRequest = new MKLocalSearchRequest();

            searchRequest.NaturalLanguageQuery = forSearchString;
            searchRequest.Region = new MKCoordinateRegion(mapa.UserLocation.Coordinate, new MKCoordinateSpan(0.25, 0.25));

            var localSearch = new MKLocalSearch(searchRequest);

            localSearch.Start(delegate(MKLocalSearchResponse response, NSError error)
            {
                if (response != null && error == null)
                {
                    this.MapItems = response.MapItems.ToList();
                    this.TableView.ReloadData();
                }
                else
                {
                    Console.WriteLine("Lokální chyba vyhledávání: {0}", error);
                }
            });
        }
Exemplo n.º 21
0
        private async Task <List <MKMapItem> > Search(string forSearchString, CancellationToken cancellationToken)
        {
            MKLocalSearchRequest request = new MKLocalSearchRequest();

            request.NaturalLanguageQuery = forSearchString;
            MKLocalSearch search = new MKLocalSearch(request);

            try
            {
                cancellationToken.ThrowIfCancellationRequested();

                MKLocalSearchResponse response = await search.StartAsync();

                cancellationToken.ThrowIfCancellationRequested();

                return(response.MapItems.ToList());
            }
            catch
            {
                return(null);
            }
        }
        public void Search(string forSearchString)
        {
            //erstelle SUchanfrage
            var searchRequest = new MKLocalSearchRequest();

            searchRequest.NaturalLanguageQuery = forSearchString;
            searchRequest.Region = new MKCoordinateRegion(mapView.UserLocation.Coordinate, new MKCoordinateSpan(0.25, 0.25));

            var localSearch = new MKLocalSearch(searchRequest);

            localSearch.Start(delegate(MKLocalSearchResponse response, NSError error)
            {
                if (response != null && error == null)
                {
                    this.MapItems = response.MapItems.ToList();
                    this.TableView.ReloadData();
                }
                else
                {
                }
            });
        }
        // TODO: Step 5a - search for an address
        private void SearchForAddress(MKMapView mapView)
        {
            var searchRequest = new MKLocalSearchRequest();

            searchRequest.NaturalLanguageQuery = "Main Street, New York, NY";
            // searchRequest.Region = new MKCoordinateRegion (mapView.UserLocation.Coordinate, new MKCoordinateSpan (0.25, 0.25));

            // perform search
            var localSearch = new MKLocalSearch(searchRequest);

            localSearch.Start(delegate(MKLocalSearchResponse response, NSError error)
            {
                if (response != null && error == null)
                {
                    var items = response.MapItems.ToList();
                    PlotItems(mapView, items);
                }
                else
                {
                    Console.WriteLine("local search error: {0}", error);
                }
            });
        }
        //krijg lat en long op basis van adres
        public void GetPosition()
        {
            var myVM = this.ViewModel as AddContactInfoViewModel;

            if (myVM != null)
            {
                //krijg lat en long op basis van adres string
                var searchRequest = new MKLocalSearchRequest();
                searchRequest.NaturalLanguageQuery = myVM.Location.ToString();
                var localSearch = new MKLocalSearch(searchRequest);
                localSearch.Start(delegate(MKLocalSearchResponse response, NSError error)
                {
                    if (response != null && error == null)
                    {
                        myVM.Location.Lat = response.MapItems[0].Placemark.Location.Coordinate.Latitude;
                        myVM.Location.Lon = response.MapItems[0].Placemark.Location.Coordinate.Longitude;
                    }
                    else
                    {
                        Console.WriteLine("local search error: {0}", error);
                    }
                });
            }
        }
Exemplo n.º 25
0
            public override bool ShouldReloadForSearchString(UISearchDisplayController controller, string forSearchString)
            {
                // create search request
                var searchRequest = new MKLocalSearchRequest();

                searchRequest.NaturalLanguageQuery = forSearchString;
                searchRequest.Region = new MKCoordinateRegion(map.UserLocation.Coordinate, new MKCoordinateSpan(0.25, 0.25));

                // perform search
                var localSearch = new MKLocalSearch(searchRequest);

                localSearch.Start(delegate(MKLocalSearchResponse response, NSError error) {
                    if (response != null && error == null)
                    {
                        ((SearchSource)controller.SearchResultsSource).MapItems = response.MapItems.ToList();
                        controller.SearchResultsTableView.ReloadData();
                    }
                    else
                    {
                        Console.WriteLine("local search error: {0}", error);

//						In "MKTypes.h" in the MapKit framework, the following is defined:
//
//							Error constants for the Map Kit framework.
//
//								enum MKErrorCode {
//								MKErrorUnknown = 1,
//								MKErrorServerFailure,
//								MKErrorLoadingThrottled,
//								MKErrorPlacemarkNotFound,
//							};
                    }
                });

                return(true);
            }
        void SearchLocal(object sender, EventArgs e)
        {
            MKLocalSearchRequest request = new MKLocalSearchRequest();

            request.NaturalLanguageQuery = CustomMapView.SearchText;
            request.Region = NativeMapView.Region;

            MKLocalSearch search = new MKLocalSearch(request);

            search.Start((MKLocalSearchResponse response, Foundation.NSError error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine(error.Description);
                }
                else if (response.MapItems.Length == 0)
                {
                    System.Diagnostics.Debug.WriteLine("No matches found for search: " + CustomMapView.SearchText);
                }
                else
                {
                    ClearRoute(null, null);

                    foreach (MKMapItem mapItem in response.MapItems)
                    {
                        MKPointAnnotation annotation = new MKPointAnnotation();
                        annotation.Coordinate        = mapItem.Placemark.Coordinate;
                        annotation.Title             = mapItem.Name;

                        annotationsList.Add(annotation);
                    }

                    NativeMapView.ShowAnnotations(annotationsList.ToArray(), true);
                }
            });
        }
			public override bool ShouldReloadForSearchString (UISearchDisplayController controller, string forSearchString)
			{
				// create search request
				var searchRequest = new MKLocalSearchRequest ();
				searchRequest.NaturalLanguageQuery = forSearchString;
				searchRequest.Region = new MKCoordinateRegion (map.UserLocation.Coordinate, new MKCoordinateSpan (0.25, 0.25));

				// perform search
				var localSearch = new MKLocalSearch (searchRequest);
				localSearch.Start (delegate (MKLocalSearchResponse response, NSError error) {
					if (response != null && error == null) {
						((SearchSource)controller.SearchResultsSource).MapItems = response.MapItems.ToList();
						controller.SearchResultsTableView.ReloadData();
					} else {
						Console.WriteLine ("local search error: {0}", error);

//						In "MKTypes.h" in the MapKit framework, the following is defined:
//
//							Error constants for the Map Kit framework.
//
//								enum MKErrorCode {
//								MKErrorUnknown = 1,
//								MKErrorServerFailure,
//								MKErrorLoadingThrottled,
//								MKErrorPlacemarkNotFound,
//							};
					}
				});

				return true;
			}
Exemplo n.º 28
0
		// Performs a natural language search for locations in the map's region that match the `searchBar`'s text.
		void PerformSearch ()
		{
			var request = new MKLocalSearchRequest ();
			request.NaturalLanguageQuery = SearchBar.Text;
			const double multiplier = RegionQueryDegreeMultiplier;
			var querySpan = new MKCoordinateSpan (MapView.Region.Span.LatitudeDelta * multiplier, MapView.Region.Span.LongitudeDelta * multiplier);
			request.Region = new MKCoordinateRegion (MapView.Region.Center, querySpan);

			var search = new MKLocalSearch (request);
			var matchingItems = new List<MKMapItem> ();

			search.Start ((response, error) => {
				MKMapItem[] mapItems = null;
				if (response != null)
					mapItems = response.MapItems ?? new MKMapItem[0];

				foreach (var item in mapItems) {
					matchingItems.Add (item);
					var annotation = new MKPointAnnotation ();
					annotation.Coordinate = item.Placemark.Coordinate;
					annotation.Title = item.Name;
					MapView.AddAnnotation (annotation);
				}
			});
		}
        // TODO: Step 5c - Search for items of interest in the current map view
        private void SearchForLocationsNearPosition(MKMapView mapView)
        {
            var searchRequest = new MKLocalSearchRequest();
            searchRequest.NaturalLanguageQuery = "Bakery";
            searchRequest.Region = mapView.Region;

            var localSearch = new MKLocalSearch(searchRequest);
            localSearch.Start (delegate (MKLocalSearchResponse response, NSError error) 
            {
                if (response != null && error == null) 
                {
                    var items = response.MapItems.ToList();
                    PlotItems(mapView, items);

                    // zoom in on the first item we plotted
                    var firstItem = items.FirstOrDefault();
                    if (firstItem != null)
                    {
                        MKCoordinateSpan span = new MKCoordinateSpan(0.8, 0.8);
                        CLLocationCoordinate2D coord = new CLLocationCoordinate2D(
                            firstItem.Placemark.Location.Coordinate.Latitude, 
                            firstItem.Placemark.Location.Coordinate.Longitude);
                        mapView.Region = new MKCoordinateRegion(coord, span);
                    }
                } 
                else 
                {
                    Console.WriteLine ("local search error: {0}", error);
                }
            });
        }
            private void LoadLocations() {

                if (_coords == null) {
                    return;
                }

                var coordinate = new CLLocationCoordinate2D(_coords.Value.Latitude, _coords.Value.Longitude);

                var localSearch = new MKLocalSearch (new MKLocalSearchRequest () {
                        NaturalLanguageQuery = "coffee",
                        Region = new MKCoordinateRegion (coordinate, new MKCoordinateSpan (1.25, 1.25))
                    });
                
                localSearch.Start ((response, error) => {
                    if (response != null && error == null) {
                        _locations = response.MapItems;
                        _parent.OnLocationsUpdate (_locations); 

                    } else {
                        Console.WriteLine ("local search error: {0}", error);
                    }
                });
            }
Exemplo n.º 31
0
        public override bool ShouldReloadForSearchString(UISearchDisplayController controller, string forSearchString)
        {
            // create search request
            var searchRequest = new MKLocalSearchRequest ();
            searchRequest.NaturalLanguageQuery = forSearchString;
            searchRequest.Region = new MKCoordinateRegion (map.UserLocation.Coordinate, new MKCoordinateSpan (0.25, 0.25));

            // perform search
            var localSearch = new MKLocalSearch (searchRequest);
            localSearch.Start (delegate (MKLocalSearchResponse response, NSError error) {
                if (response != null && error == null) {
                    ((SearchSource)controller.SearchResultsSource).MapItems = GetList(response.MapItems);
                    controller.SearchResultsTableView.ReloadData();
                } else {
                    Console.WriteLine ("local search error: {0}", error);
                }
            });

            return true;
        }