-2

I created a MapsActivity and corresponding activity_maps.xml.My activity_main.xml there will be a button which when tapped should show the current location pointed by a marker.

Code works fine when making the MapsActivity as Launcher Activity in AndroidManifest.xml but want my MainActivity to be the Launcher Activity.

how I can make my MainActivity as Launcher Activity.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;

 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);
}

public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(this, "inside onMapReady()", Toast.LENGTH_SHORT).show();//doesn't appear
    Log.i("hurray:","inside onMapReady()");//doesn't appear
    mMap = googleMap;
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(@NonNull Location location) {
            if(location != null){
                Toast.makeText(MapsActivity.this, "Location is not null", Toast.LENGTH_SHORT).show();//doesn't appear
                LatLng userLatLngLocation = new LatLng(location.getLatitude(),location.getLongitude());
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(userLatLngLocation);
                markerOptions.title("Current position");
                markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
                mMap.addMarker(markerOptions);
                mMap.moveCamera(CameraUpdateFactory.newLatLng(userLatLngLocation));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
                Toast.makeText(getApplicationContext(), location.toString(), Toast.LENGTH_SHORT).show();//does appears sometimes
            }
            else{
                Toast.makeText(getApplicationContext(), "location is null !", Toast.LENGTH_SHORT).show();
            }
        }
public void requestLocationPermission() {
        //if permission not granted ask for it
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
        //else if location permission is already granted get location updates
        else {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);//every 0secs & 0meters
    }
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int grantResults[]) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                }
            }
        }
    }

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    Button getPopLocationButton;//button to show the Maps
    MapsActivity mapsActivity;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_screen);
        mapsActivity = new MapsActivity();
        getPopLocationButton = findViewById(R.id.buttonPopLocation); 
                                                                    
        getPopLocationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.activity_maps); 
            }
        });
    }
}
Ubaid Khan
  • 7
  • 1
  • 8
  • Can you check [this solution](https://stackoverflow.com/questions/22471100/how-to-show-current-position-marker-on-map-in-android) in old question – Ahmed Nezhi Oct 19 '20 at 13:57
  • @Ahmed didn't worked :( – Ubaid Khan Oct 19 '20 at 15:41
  • Okay i just noticed that if i make the MapsActivity the launcher activity in AndroidManifest.xml the code works fine and a marker is visible at current user location but i want to have my MainActivity as my launcher activity and activity_main.xml should have a button called "show current location" which when tapped should show this Marker. – Ubaid Khan Oct 20 '20 at 09:03

1 Answers1

0

Okay I got this working,

   public void onClick(View view){
    setContentView(R.layout.map); 
 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(MainActivity.this);}
Ubaid Khan
  • 7
  • 1
  • 8