2017-02-24 77 views
0

在Google地圖iOS版有一個明確的說明中關於如何使用搜索詞搜索地圖導遊:在Xamarin.Android中,有一種簡單的方法可以根據搜索詞而不是LatLng查找地圖位置?

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

但是每一個Android的例子,我所看到的是不是使用的座標。

有沒有簡單的等價物爲Android使用搜索詞?

謝謝

回答

-1

您可以使用Foursquare API按名稱搜索地點。該API將給你的座標。

https://developer.foursquare.com/

在Android中,你可以給座標,並得到ADRES名單,但如果你想反向方法,你應該使用支持此類似Foursquare的一個API。

0

您可以使用Google的Places API。

注意:您的在您的示例中使用lat/long來定義搜索區域。

mGoogleApiClient = new GoogleApiClient 
    .Builder(this) 
     .AddApi(PlacesClass.GEO_DATA_API) 
     .AddApi(PlacesClass.PLACE_DETECTION_API) 
     .AddApi(LocationServices.API) 
     .EnableAutoManage(this, this) 
     .Build(); 
mGoogleApiClient.BlockingConnect(); 
var searchText = "Starbucks Coffee"; 
var latLngBuilder = LatLngBounds.InvokeBuilder(); 
var currentLocation = new LatLng(47.60357, -122.3295); // use Seattle, WA as default if Fused location returns null 
var mLastLocation = LocationServices.FusedLocationApi.GetLastLocation(mGoogleApiClient); 
if (mLastLocation != null) 
{ 
    currentLocation.Latitude = mLastLocation.Latitude; 
    currentLocation.Longitude = mLastLocation.Longitude; 
} 
var zoomFactor = 0.005f; // ~ a few city block... adjust as needed... 
latLngBuilder.Include(new LatLng(currentLocation.Latitude - zoomFactor, currentLocation.Longitude - zoomFactor)); 
latLngBuilder.Include(new LatLng(currentLocation.Latitude + zoomFactor, currentLocation.Longitude + zoomFactor)); 
var latLngBounds = latLngBuilder.Build(); 

var results = await PlacesClass.GeoDataApi.GetAutocompletePredictionsAsync(mGoogleApiClient, searchText, latLngBounds, null); 
if (results.Status.IsSuccess) 
{ 
    Log.Debug("SO", $"{results.Count} {searchText} results were found close to your location"); 
    foreach (var item in results) 
    { 
     Log.Debug("SO", $"{item.PlaceId}"); 
    } 
} 
else 
    Log.Error("SO", results.Status.StatusMessage); 
相關問題