Gmail - FWD - Google Maps
Gmail - FWD - Google Maps
1 message
mainactivity
package com.example.googlemap;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import com.example.labp.R;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.annotation.SuppressLint;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.Priority;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private FusedLocationProviderClient fusedLocationClient;
private LocationRequest locationRequest;
private LocationCallback locationCallback;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
// Modern LocationRequest using Builder API
locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
.setMinUpdateIntervalMillis(5000)
.build();
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(@NonNull LocationResult locationResult) {
if (!locationResult.getLocations().isEmpty()) {
Location location = locationResult.getLastLocation();
if (location != null) {
updateMap(location);
}
}
}
};
// Initialize map safely
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
@SuppressLint("MissingPermission")
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
LatLng india = new LatLng(20.5937, 78.9629);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(india, 5f));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// Request location permission if not granted
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.A
CCESS_FINE_LOCATION}, LOCATION_PERMISSION_REQUEST_CODE);
return;
}
mMap.setMyLocationEnabled(true);
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback,
getMainLooper());
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
onMapReady(mMap); // Retry map setup with permission granted
}
}
}
private void updateMap(Location location) {
LatLng userLatLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear(); // Optional: clear old markers
mMap.addMarker(new MarkerOptions().position(userLatLng).title("You are here"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLatLng, 15f));
}
}
manifest.xml
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Googlemap"
tools:targetApi="31">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name="com.example.googlemap.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
google_map_api.xml
.xml
</LinearLayout>