2015-09-02 40 views
4

我的目標是使用Google Places API進行自動完成預測,現在我想製作一些算法,將當前的位置lat和lng進行預測,並對地點進行預測只有100-200公里的直徑。如何根據當前位置設置正確的緯度和長度

那麼,在這一刻我得到用戶當前的位置lat和lng,如何設置100-200公里?

private void getCurrentLocation() { 
    mLastLocation = LocationServices.FusedLocationApi 
      .getLastLocation(mGoogleApiClient); 
    if (mLastLocation != null) { 
     double latitude = mLastLocation.getLatitude(); 
     double longitude = mLastLocation.getLongitude(); 
     mLatLonBounds = new LatLngBounds(new LatLng(latitude,longitude), 
            new LatLng(latitude,longitude)); 
     Log.d("myTag","lat = "+mLatLonBounds.northeast.latitude+" ,lon = "+mLatLonBounds.northeast.longitude); 
     //Log.d("myTag","lat = "+mLatLonBounds.southwest.latitude+" ,lon = "+mLatLonBounds.southwest.longitude); 

    }else { 
     //some code 
    } 
} 

這是我如何設置範圍爲自動預測:

@Nullable 
private ArrayList<AutoCompletePlace> getAutocomplete(CharSequence constraint) { 
    if (mGoogleApiClient.isConnected()) { 
     Log.i(Constants.AUTO_COMPLETE_TAG, "Starting autocomplete query for: " + constraint); 

     // Submit the query to the autocomplete API and retrieve a PendingResult that will 
     // contain the results when the query completes. 
     PendingResult<AutocompletePredictionBuffer> results = Places.GeoDataApi 
         .getAutocompletePredictions(mGoogleApiClient, constraint.toString(), 
           **mBounds**, mPlaceFilter); 

     // This method should have been called off the main UI thread. Block and wait for at most 60s 
     // for a result from the API. 
     AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS); 

     // Confirm that the query completed successfully, otherwise return null 
     final Status status = autocompletePredictions.getStatus(); 
     if (!status.isSuccess()) { 
      Toast.makeText(getContext(), "Error contacting API: " + status.toString(), 
        Toast.LENGTH_SHORT).show(); 
      Log.e(Constants.AUTO_COMPLETE_TAG, "Error getting autocomplete prediction API call: " + status.toString()); 
      autocompletePredictions.release(); 
      return null; 
     } 

     Log.i(Constants.AUTO_COMPLETE_TAG, "Query completed. Received " + autocompletePredictions.getCount() 
       + " predictions."); 

     // Copy the results into our own data structure, because we can't hold onto the buffer. 
     // AutocompletePrediction objects encapsulate the API response (place ID and description). 

     Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator(); 
     ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount()); 
     while (iterator.hasNext()) { 
      AutocompletePrediction prediction = iterator.next(); 
      // Get the details of this prediction and copy it into a new PlaceAutocomplete object. 
      resultList.add(new AutoCompletePlace(prediction.getPlaceId(), 
        prediction.getDescription())); 
     } 

     // Release the buffer now that all data has been copied. 
     autocompletePredictions.release(); 

     return resultList; 
    } 
    Log.e(Constants.AUTO_COMPLETE_TAG, "Google API client is not connected for autocomplete query."); 
    return null; 

比如我的當前位置48.6180288,22.2984587。

更新:在Francois Wouts給我答案之前,我在stackoverflow上找到了另一個解決方案,你也可以使用它。

public static final LatLngBounds setBounds(Location location, int mDistanceInMeters){ 
    double latRadian = Math.toRadians(location.getLatitude()); 
    double degLatKm = 110.574235; 
    double degLongKm = 110.572833 * Math.cos(latRadian); 
    double deltaLat = mDistanceInMeters/1000.0/degLatKm; 
    double deltaLong = mDistanceInMeters/1000.0/degLongKm; 

    double minLat = location.getLatitude() - deltaLat; 
    double minLong = location.getLongitude() - deltaLong; 
    double maxLat = location.getLatitude() + deltaLat; 
    double maxLong = location.getLongitude() + deltaLong; 

    Log.d("Location", "Min: " + Double.toString(minLat) + "," + Double.toString(minLong)); 
    Log.d("Location","Max: "+Double.toString(maxLat)+","+Double.toString(maxLong)); 

    // Set up the adapter that will retrieve suggestions from the Places Geo Data API that cover 
    // the entire world. 

    return new LatLngBounds(new LatLng(minLat,minLong),new LatLng(maxLat,maxLong)); 
+0

您是否檢查過[this?](https://developers.google.com/places/android-api/autocomplete) – makata

回答

4

根據Wikipedia,你可能想要允許用戶位置周圍每個方向大約1度,以覆蓋100-200km。覆蓋的確切區域將取決於用戶的位置,但對於大多數使用情況來說,這應該是一個足夠好的近似值。

嘗試以下,例如:

double radiusDegrees = 1.0; 
LatLng center = /* the user's location */; 
LatLng northEast = new LatLng(center.latitude + radiusDegrees, center.longitude + radiusDegrees); 
LatLng southWest = new LatLng(center.latitude - radiusDegrees, center.longitude - radiusDegrees); 
LatLngBounds bounds = LatLngBounds.builder() 
     .include(northEast) 
     .include(southWest) 
     .build(); 

我相信這應該乃至全國antemeridian正常工作。讓我知道你怎樣去!

+0

感謝您的回答,我會檢查這個,'center'是一個'Location'對象? –

+0

這是一個LatLng,對不起,在初始答案中不清楚。 –

+0

它的工作原理,謝謝! –

相關問題