Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

App doesn't show MyLocationView in the map #449

Closed
Guardiola31337 opened this issue Aug 14, 2017 · 2 comments
Closed

App doesn't show MyLocationView in the map #449

Guardiola31337 opened this issue Aug 14, 2017 · 2 comments

Comments

@Guardiola31337
Copy link
Contributor

Per mapbox/mapbox-gl-native#9618 wear module was removed from gl-native.
Opening a ticket for follow up work/tickets don't get lost.

Related tickets:
lostzen/lost#176
lostzen/lost#177

@FelipeRRM
Copy link

I'm also developing a Wear app and I'm struggling to get this working.

@Override
    public void onMapReady(MapboxMap mapboxMap) {
        mMap = mapboxMap;
        mMap.setMyLocationEnabled(true);
}

Does not work, but then I read it has just been deprecated, then I tried the new suggested method:

@Override
public void onMapReady(MapboxMap mapboxMap) {
        mMap = mapboxMap;
        if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, MAPFRAGMENT_PERMISSION_REQUEST);
            return;
        } else {
            setUpLocationEngine();
        }
}

private void setUpLocationEngine() {
    if (mMapView != null && mMap != null) {
            locationEngine = LostLocationEngine.getLocationEngine(getContext());
            locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
            locationEngine.activate();
            locationPlugin = new LocationLayerPlugin(mMapView, mMap, locationEngine);
            locationPlugin.setLocationLayerEnabled(LocationLayerMode.COMPASS);
            locationPlugin.applyStyle(R.style.CustomLocationLayer);
        }
    }

Which, unfortunately, does not work either. With this I get a fatal exception:

FATAL EXCEPTION: main com.mapbox.mapboxsdk.style.sources.CannotAddSourceException: Source mapbox-location-source already exists

@FelipeRRM
Copy link

FelipeRRM commented Aug 18, 2017

Managed to get it working! Just changed the Location Engine to one based on Google Play Services and it worked:

locationEngine = GoogleLocationEngine.getLocationEngine(getContext());

I also had to remove that last line that was crashing.

This Location Engine is included in the demo project, but here it is for reference:

public class GoogleLocationEngine extends LocationEngine implements
  GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

  private static final String LOG_TAG = GoogleLocationEngine.class.getSimpleName();

  private static LocationEngine instance;

  private WeakReference<Context> context;
  private GoogleApiClient googleApiClient;

  public GoogleLocationEngine(Context context) {
    super();
    this.context = new WeakReference<>(context);
    googleApiClient = new GoogleApiClient.Builder(this.context.get())
      .addConnectionCallbacks(this)
      .addOnConnectionFailedListener(this)
      .addApi(LocationServices.API)
      .build();
  }

  public static synchronized LocationEngine getLocationEngine(Context context) {
    if (instance == null) {
      instance = new GoogleLocationEngine(context.getApplicationContext());
    }

    return instance;
  }

  @Override
  public void activate() {
    if (googleApiClient != null && !googleApiClient.isConnected()) {
      googleApiClient.connect();
    }
  }

  @Override
  public void deactivate() {
    if (googleApiClient != null && googleApiClient.isConnected()) {
      googleApiClient.disconnect();
    }
  }

  @Override
  public boolean isConnected() {
    return googleApiClient.isConnected();
  }

  @Override
  public void onConnected(@Nullable Bundle bundle) {
    for (LocationEngineListener listener : locationListeners) {
      listener.onConnected();
    }
  }

  @Override
  public void onConnectionSuspended(int cause) {
    Log.d(LOG_TAG, "Connection suspended: " + cause);
  }

  @Override
  public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(LOG_TAG, "Connection failed:" + connectionResult.getErrorMessage());
  }

  @Override
  public Location getLastLocation() {
    if (googleApiClient.isConnected()) {
      //noinspection MissingPermission
      return LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    }

    return null;
  }

  @Override
  public void requestLocationUpdates() {
    // Create the LocationRequest object
    LocationRequest locationRequest = LocationRequest.create();
    // Use high accuracy
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    // Set the update interval to 2 seconds
    locationRequest.setInterval(TimeUnit.SECONDS.toMillis(2));
    // Set the fastest update interval to 2 seconds
    locationRequest.setFastestInterval(TimeUnit.SECONDS.toMillis(2));
    // Set the minimum displacement
    locationRequest.setSmallestDisplacement(2);

    // Register listener using the LocationRequest object
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
  }

  @Override
  public void removeLocationUpdates() {
    if (googleApiClient.isConnected()) {
      LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
    }
  }

  @Override
  public void onLocationChanged(Location location) {
    for (LocationEngineListener listener : locationListeners) {
      listener.onLocationChanged(location);
    }
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants