-5

I'm trying to set up a Google Maps activity in Android Studio that pulls User location and displays it with a marker.

I have tried different ways to pull user location and saving it as variables but the applications always crash when used on the emulator.


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private Location location;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(latLng).title("You"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

}

}

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Mar 26 '19 at 15:37

1 Answers1

0

If the app works on a real phone, and not on an emulator:

if you have not loaded google api's on the the emulator that means that maps are not installed on it. So in sdk manager, view details, install play services & google apis for the level you need. Note: it is not available on older images. Then use that for your device.