*************Java Code--------------**************
package com.example.mygooglemapapp;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
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.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private G oogleMap mMap;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
fab = findViewById(R.id.fab);
// Obtain the SupportMapFragment and get notified when the map is ready to be
used.
upportMapFragment mapFragment = (SupportMapFragment)
S
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In
this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be
prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the
user has
* installed Google Play services and returned to the app.
*/
@ Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mMap.getMapType() == GoogleMap.MAP_TYPE_HYBRID){
fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
R.drawable.ic_visibility) );
// Sets the map type to be "hybrid"
Map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
m
} else if (mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL){
fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
R.drawable.ic_visibility_off));
// Sets the map type to be "normal"
Map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
m
}
}
});
mMap.setOnMyLocationButtonClickListener(onMyLocationButtonClickListener);
mMap.setOnMyLocationClickListener(onMyLocationClickListener);
enableMyLocationIfPermitted();
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMinZoomPreference(11);
}
private void enableMyLocationIfPermitted() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
private void showDefaultLocation() {
Toast.makeText( this, "Location permission not granted, " +
"showing default location",
Toast.LENGTH_SHORT).show();
LatLng redmond = new LatLng(47.6739881, -122.121512);
mMap.moveCamera(CameraUpdateFactory.newLatLng(redmond));
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableMyLocationIfPermitted();
} else {
showDefaultLocation();
}
return;
}
}
}
private GoogleMap.OnMyLocationButtonClickListener onMyLocationButtonClickListener
=
new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
mMap.setMinZoomPreference(15);
return false;
}
};
private GoogleMap.OnMyLocationClickListener onMyLocationClickListener =
new GoogleMap.OnMyLocationClickListener() {
@Override
public void onMyLocationClick(@NonNull Location location) {
mMap.setMinZoomPreference(12);
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(new LatLng(location.getLatitude(),
location.getLongitude()));
circleOptions.radius(200);
circleOptions.fillColor(Color.RED);
circleOptions.strokeWidth(6);
mMap.addCircle(circleOptions);
}
};
}
?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:con******************xml code****************
<text=".MapsActivity">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
android:clickable="true"
android:src="@drawable/ic_visibility"
app:backgroundTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />