0

Using Default Google Maps Activity from Android Studio and want to use MY Location displayed on a marker on a map plus the my location blue google button which is usually located bottom right.

Have looked over hundreds of examples and code and nothing is working.

   package com.example.br.nightlyfegooglemap;

  public class MapsActivity extends FragmentActivity implements    OnMapReadyCallback,OnInfoWindowClickListener,
     OnMyLocationChangeListener, GoogleMap.OnMyLocationChangeListener   {

private GoogleMap mMap;
protected GoogleApiClient mGoogleApiClient;
private static final String STATE_IN_PERMISSION="inPermission";
private static final int REQUEST_PERMS=1337;
private boolean needsInit=false;
private boolean isInPermission=false;

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

    if (savedInstanceState==null) {
        needsInit=true;
    }
    else {
        isInPermission=
                savedInstanceState.getBoolean(STATE_IN_PERMISSION, false);
    }

    onCreateForRealz(canGetLocation());

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putBoolean(STATE_IN_PERMISSION, isInPermission);
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String[] permissions,
                                       int[] grantResults) {
    isInPermission=false;

    if (requestCode==REQUEST_PERMS) {
        if (canGetLocation()) {
            onCreateForRealz(true);
        }
        else {
            finish(); // denied permission, so we're done
        }
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    if (needsInit) {
        CameraUpdate center=CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,-73.98180484771729));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

        mMap.moveCamera(center);
        mMap.animateCamera(zoom);
    }



    mMap.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
    mMap.setOnInfoWindowClickListener(this);

    mMap.setMyLocationEnabled(true);
    mMap.setOnMyLocationChangeListener(this);

}

public void onInfoWindowClick(Marker marker) {
    Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
}

@Override
public void onMyLocationChange(Location lastKnownLocation) {
    Log.d(getClass().getSimpleName(),
            String.format("%f:%f", lastKnownLocation.getLatitude(),
                    lastKnownLocation.getLongitude()));
}

private void onCreateForRealz(boolean canGetLocation) {
    if (canGetLocation) {
        if (readyToGo()) {
            setContentView(R.layout.activity_maps);

            MapFragment mapFrag=
                    (MapFragment)getFragmentManager().findFragmentById(
                            R.id.map);

            mapFrag.getMapAsync(this);
        }
    }
    else if (!isInPermission) {
        isInPermission=true;

        ActivityCompat.requestPermissions(this,
                new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_PERMS);
    }
}

private void addMarker(GoogleMap map, double lat, double lon,
                       int title, int snippet) {
    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
            .title(getString(title))
            .snippet(getString(snippet)));
}

private boolean canGetLocation() {
    return(ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)==
            PackageManager.PERMISSION_GRANTED);
}
}

ERRORS

   //CANNOT RESOLVE SYMBOLS FOR THESE
    OnInfoWindowClickListener,
    OnMyLocationChangeListener
    new PopupAdapter
    readyToGo

   //REQUIRES PERMISSION
     mMap.setMyLocationEnabled(true);

   //DEPRECATED
     mMap.setOnMyLocationChangeListener(this);
Ordte
  • 1
  • 1
  • If your app is crashing, use LogCat to examine the Java stack trace associated with that crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare May 14 '16 at 14:11
  • Its crashing because of that setmylocation(true). – Ordte May 14 '16 at 14:12
  • If you look under your question, you will find an "edit" link. Please click that link and edit your question to include the complete stack trace. – CommonsWare May 14 '16 at 14:16
  • @CommonsWare Done, check it please. – Ordte May 14 '16 at 14:27

0 Answers0