Java源码示例:com.mapzen.android.lost.api.LostApiClient
示例1
@Override
public void onReceive(Context context, Intent intent) {
prefs = Utils.getPrefs(context);
switch (intent.getAction()) {
case Intent.ACTION_BOOT_COMPLETED:
case ACTION_START_LOCATION:
tempContext = context;
apiClient = new LostApiClient.Builder(context)
.addConnectionCallbacks(this)
.build();
apiClient.connect();
break;
case ACTION_LOCATION_UPDATE:
if (prefs.getBoolean(Common.PREF_ENABLE_LOCATION_TRACKING, false) && LocationResult.hasResult(intent)) {
LocationResult result = LocationResult.extractResult(intent);
Location location = result.getLastLocation();
if (location != null)
logLocation(location, context);
}
break;
}
}
示例2
@Test
public void getBoundingBox_lostClient_shouldReturnCorrectBox() throws Exception {
FusedLocationProviderApiImpl impl =
(FusedLocationProviderApiImpl) LocationServices.FusedLocationApi;
IFusedLocationProviderService service = mock(IFusedLocationProviderService.class);
FusedApiServiceUpdater.updateApiService(impl, service);
LostApiClient client = mock(LostApiClient.class);
when(client.isConnected()).thenReturn(true);
presenter.setLostClient(client);
Location location = mock(Location.class);
when(location.getLatitude()).thenReturn(40.0);
when(location.getLongitude()).thenReturn(70.0);
when(service.getLastLocation()).thenReturn(location);
BoundingBox boundingBox = presenter.getBoundingBox();
double boundsRadius = 0.02;
assertThat(boundingBox.getMinLat() + boundsRadius).isEqualTo(location.getLatitude());
assertThat(boundingBox.getMinLon() + boundsRadius).isEqualTo(location.getLongitude());
assertThat(boundingBox.getMaxLat() - boundsRadius).isEqualTo(location.getLatitude());
assertThat(boundingBox.getMaxLon() - boundsRadius).isEqualTo(location.getLongitude());
}
示例3
/**
* Set client to be used to retrieve device location if bounds is not set.
* @param client
*/
void setLostClient(LostApiClient client) {
this.client = client;
if (this.client != null) {
client.connect();
}
}
示例4
private void connectClient() {
client = new LostApiClient.Builder(this).addConnectionCallbacks(
new LostApiClient.ConnectionCallbacks() {
@Override public void onConnected() {
queryAutocompleteApi();
}
@Override public void onConnectionSuspended() {
}
}).build();
client.connect();
}
示例5
/**
* Create a new {@link OverlayManager} object for handling functionality between map and
* location services.
*/
OverlayManager(MapView mapView, MapController mapController, MapDataManager mapDataManager,
MapStateManager mapStateManager, LostApiClient lostApiClient) {
if (lostApiClient == null) {
lostApiClient = new LostApiClient.Builder(mapView.getContext())
.addConnectionCallbacks(connectionCallbacks).build();
}
this.mapView = mapView;
this.mapController = mapController;
this.mapDataManager = mapDataManager;
this.mapStateManager = mapStateManager;
this.lostApiClient = lostApiClient;
}
示例6
@Before public void setup() throws Exception {
mapController = mock(TestMapController.class);
lostApiClient = mock(LostApiClient.class);
mapView = mock(MapView.class);
mapData = new TestMapData("test");
setFinalStatic(LocationServices.class.getDeclaredField("FusedLocationApi"),
Mockito.mock(FusedLocationProviderApiImpl.class));
mapDataManager = new MapDataManager();
mapStateManager = mock(MapStateManager.class);
overlayManager = new OverlayManager(mapView, mapController, mapDataManager, mapStateManager,
lostApiClient);
when(LocationServices.FusedLocationApi.getLastLocation(lostApiClient))
.thenReturn(new Location("test"));
when(lostApiClient.isConnected()).thenReturn(true);
compassButton = new TestCompassButton(mockContext);
findMeButton = new TestButton(null);
zoomInButton = new TestButton(null);
zoomOutButton = new TestButton(null);
when(mapController.addDataLayer(any(String.class))).thenReturn(mapData);
when(mapView.showCompass()).thenReturn(compassButton);
when(mapView.findViewById(R.id.mz_compass)).thenReturn(compassButton);
when(mapView.showFindMe()).thenReturn(findMeButton);
when(mapView.showZoomIn()).thenReturn(zoomInButton);
when(mapView.showZoomOut()).thenReturn(zoomOutButton);
when(mapView.findViewById(R.id.mz_find_me)).thenReturn(findMeButton);
when(mapView.findViewById(R.id.mz_zoom_in)).thenReturn(zoomInButton);
when(mapView.findViewById(R.id.mz_zoom_out)).thenReturn(zoomOutButton);
}
示例7
@Test public void sharedClientWithCallbacks_shouldReturnSameClientForSameContext()
throws Exception {
Context context = Mockito.mock(Context.class);
LostApiClient.ConnectionCallbacks callbacks = new TestConnectionCallbacks();
assertThat(LocationFactory.sharedClient(context, callbacks))
.isEqualTo(LocationFactory.sharedClient(context, callbacks));
}
示例8
@Override public PendingResult<AutocompletePredictionBuffer> getAutocompletePredictions(
LostApiClient client, String query, LatLngBounds bounds, AutocompleteFilter filter) {
return new AutocompletePendingResult(pelias, query, bounds, filter);
}
示例9
/**
* Returns a new instance of {@link LostApiClient}.
*/
public LostApiClient getApiClient(Context context) {
return new LostApiClient.Builder(context).build();
}
示例10
@Test public void sharedClientWithCallbacks_shouldReturnSameClientForNewContext()
throws Exception {
LostApiClient.ConnectionCallbacks callbacks = new TestConnectionCallbacks();
assertThat(LocationFactory.sharedClient(Mockito.mock(Context.class), callbacks))
.isEqualTo(LocationFactory.sharedClient(Mockito.mock(Context.class), callbacks));
}
示例11
@Provides @Singleton LostApiClient provideLocationClient() {
return new LostApiClient.Builder(application).build();
}
示例12
/**
* Returns shared {@link LostApiClient}.
*
* @deprecated This method should not be used outside the SDK. Now that Lost has proper support
* for multiple location clients exposing a shared client in no longer needed. Behavior has been
* updated to create a new client instance for each new {@link Context}. This fixes a bug
* where the shared client was created with a {@link Context} that had been subsequently destroyed
* and future attempts to bind the fused location service would fail.
*/
@Deprecated public static LostApiClient sharedClient(Context context) {
if (shared == null) {
shared = new LostApiClient.Builder(context).build();
}
return shared;
}
示例13
/**
* Returns shared {@link LostApiClient} with {@link ConnectionCallbacks}.
*
* @deprecated This method should not be used outside the SDK. Now that Lost has proper support
* for multiple location clients exposing a shared client in no longer needed. Behavior has been
* updated to create a new client instance for each new {@link Context}. This fixes a bug
* where the shared client was created with a {@link Context} that had been subsequently destroyed
* and future attempts to bind the fused location service would fail.
*/
@Deprecated public static LostApiClient sharedClient(Context context,
ConnectionCallbacks callbacks) {
if (shared == null) {
shared = new LostApiClient.Builder(context).addConnectionCallbacks(callbacks).build();
}
return shared;
}
示例14
/**
* Returns an object which can be used to retrieve autocomplete results.
* @param client
* @param query
* @param bounds
* @param filter
* @return
*/
@NonNull PendingResult<AutocompletePredictionBuffer> getAutocompletePredictions(
@NonNull LostApiClient client, @NonNull String query, @NonNull LatLngBounds bounds,
@NonNull AutocompleteFilter filter);