So, I am creating an App, where I am adding multiple Geo-Fencing. But, as my current location is moving but the map is not moving, so the current location is going outside the map view. To summarize, the current location dot is not in the center of the map while it's moving. So, what changes I need to make. Kindly, reply.
Here is my Hotspot.class (Java)
package com.example.testaid;
public class Hotspot extends AppCompatActivity implements OnMapReadyCallback, GeoQueryEventListener {
private GoogleMap mMap;
private FusedLocationProviderClient mFusedLocationProviderClient;
private PlacesClient placesClient;
private List<AutocompletePrediction> predictionList;
private DatabaseReference myLocationRef;
private GeoFire geoFire;
private List<LatLng> dangerousArea;
private Location mLastKnownLocation;
private LocationCallback locationCallback;
private MaterialSearchBar materialSearchBar;
private View mapView;
private final float DEFAULT_ZOOM = 18;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotspot);
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Maps
materialSearchBar = findViewById(R.id.searchBar);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
settingGeoFire();
initArea();
mapView = mapFragment.getView();
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(Hotspot.this);
Places.initialize(Hotspot.this, "Your API key");
placesClient = Places.createClient(this);
AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
//Bottom Navigation Bar
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
bottomNavigationView.setSelectedItemId(R.id.hotspot);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home:
startActivity(new Intent(getApplicationContext()
, MainActivity.class));
overridePendingTransition(0, 0);
return true;
case R.id.cars:
startActivity(new Intent(getApplicationContext()
, Shipping.class));
overridePendingTransition(0, 0);
return true;
case R.id.news:
startActivity(new Intent(getApplicationContext()
, News.class));
overridePendingTransition(0, 0);
return true;
}
return false;
}
});
}
private void initArea() {
dangerousArea = new ArrayList<>();
dangerousArea.add(new LatLng(x,y));
}
private void settingGeoFire() {
myLocationRef = FirebaseDatabase.getInstance().getReference("My Location");
geoFire = new GeoFire(myLocationRef);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.isBuildingsEnabled();
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
if (mapView !=null && mapView.findViewById(Integer.parseInt("1")) != null) {
View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.setMargins(0,0,40,180);
}
if (mapView !=null && mapView.findViewById(Integer.parseInt("1")) != null) {
View locationCompass = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("5"));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationCompass.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
layoutParams.setMargins(0,200,30,0);
}
//check if location is enabled
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(3000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient settingsClient = LocationServices.getSettingsClient(Hotspot.this);
Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(builder.build());
task.addOnSuccessListener(Hotspot.this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
getDeviceLocation();
}
});
task.addOnFailureListener(Hotspot.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException){
ResolvableApiException resolvable = (ResolvableApiException) e;
try {
resolvable.startResolutionForResult(Hotspot.this, 51);
} catch (IntentSender.SendIntentException e1) {
e1.printStackTrace();
}
}
}
});
for (LatLng latLng :dangerousArea )
{
mMap.addCircle(new CircleOptions().center(latLng)
.radius(200)
.strokeColor(Color.RED)
.fillColor(0x220000FF)
.strokeWidth(5.0f)
);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latLng.latitude,latLng.longitude),0.2f); // 500m
geoQuery.addGeoQueryEventListener(Hotspot.this);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 51) {
if (requestCode == RESULT_OK) {
getDeviceLocation();
}
}
}
private void getDeviceLocation (){
mFusedLocationProviderClient.getLastLocation()
.addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()){
mLastKnownLocation = task.getResult();
if(mLastKnownLocation != null){
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
} else {
final LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(3000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult == null){
return;
}
mLastKnownLocation = locationResult.getLastLocation();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
mFusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
};
mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
}
} else {
Toast.makeText(Hotspot.this, "Unable to Get Last Location", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onKeyEntered(String key, GeoLocation location) {
sendNotification("TestAid",String.format("%s entered the dangerous area ", key));
}
@Override
public void onKeyExited(String key) {
sendNotification("TestAid",String.format("%s leave the dangerous area ", key));
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
sendNotification("TestAid",String.format("%s move within the dangerous area ", key));
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
Toast.makeText(this, ""+error.getMessage(), Toast.LENGTH_SHORT).show();
}
private void sendNotification(String title, String content) {
Toast.makeText(this, ""+content, Toast.LENGTH_SHORT).show();
String NOTIFICATION_CHANNEL_ID = "TESTAID_MULTIPLE_LOCATION";
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Channel Description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[] {0,1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
builder.setContentTitle(title)
.setContentText(content)
.setAutoCancel(false)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
Notification notification = builder.build();
notificationManager.notify(new Random().nextInt(),notification);
}
}
There are two XML Layout, one is Content Layout, other is Activity Layout
Activity Layout
<?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"
android:background="#00FFFF00"
tools:context=".Hotspot"
android:fitsSystemWindows="true">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="55dp"
android:elevation="10dp"
android:background="@drawable/rounded"
app:itemIconTint="@color/nav_color"
app:itemTextColor="@color/nav_color"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/menu_bottom" />
<include layout="@layout/content_hotspot_"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Content Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Hotspot"
tools:showIn="@layout/activity_hotspot"
android:fitsSystemWindows="true">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.mancj.materialsearchbar.MaterialSearchBar
style="@style/MaterialSearchBarLight"
app:mt_speechMode="false"
app:mt_hint="Search"
app:mt_maxSuggestionsCount="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchBar"
android:layout_margin="16dp"
app:mt_placeholder="Search"
android:outlineProvider="bounds"
android:elevation="5dp"/>
</RelativeLayout>