0

We're trying to get the current location to show as a marker on the map, however it sets the location to 0,0 (long,lat) instead.

On the phone we get a notification saying "Searching for GPS" whenever we run the app.

This is all the code regarding permissions for GPS & the actual location requests:

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleApiClient.OnConnectionFailedListener , GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient googleApiClient;
private Location mijnLocation;
private LatLng latLng;
private LocationManager locationManager;

private double latitude;
private double longitude;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawerlayout);

    if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }

    addMarkerButton();

    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    googleApiClient.connect();

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    mapFragment.getMapAsync(this);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
        mijnLocation = location;
    }
}


@Override
public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude =  location.getLongitude();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    if(mijnLocation == null){
        mijnLocation = new Location("GPS");
        mijnLocation.setLatitude(latitude);
        mijnLocation.setLongitude(longitude);
        latLng = new LatLng(mijnLocation.getLatitude(),mijnLocation.getLongitude());
    }
    else{
        latLng = new LatLng(mijnLocation.getLatitude(),mijnLocation.getLongitude());
    }

    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

    googleMap.addMarker(new MarkerOptions().position(latLng).title("AP Hogeschool"));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}


@Override
public void onConnected(@Nullable Bundle bundle) {
   Log.v("google api connected", Boolean.toString(googleApiClient.isConnected()));

    try{

        try{
            mijnLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        }

        catch (NullPointerException e){
            Log.v("error", "spoof toegevoegd");
            mijnLocation = new Location("GPS");
            mijnLocation.setLatitude(51.229791);
            mijnLocation.setLongitude(4.416028);
        }

        if(mijnLocation == null){
            Log.v("error", "spoof toegevoegd");
            mijnLocation = new Location("GPS");
            mijnLocation.setLatitude(51.229791);
            mijnLocation.setLongitude(4.416028);
        }
    }
    catch(SecurityException e){
        Log.v("LOCATION ERROR", e.toString());
    }
}

(In the last part we're also adding a "spoofed" location in case it can't find the current location or access the GPS, but it doesn't come that far)

There's a few more methods, but they don't affect the location. I can provide more code if necessary. Thank you

Axelle
  • 442
  • 1
  • 9
  • 24
  • The only way to guarantee a Location is to request one. The `getLastLocation()` method will often return null if no other app has recently explicitly requested a location. – Daniel Nugent Jan 12 '17 at 22:45
  • 1
    Oh I understand now.. Thank you for pointing me to the other question! I'll use the info provided there :) Sorry for the duplicate. – Axelle Jan 12 '17 at 23:21

0 Answers0