gpt4 book ai didi

java - 我希望当用户使用 GPS 进入 10 米半径时收到通知

转载 作者:行者123 更新时间:2023-12-01 17:43:39 24 4
gpt4 key购买 nike

我已经显示了当前位置,获取了纬度和经度。使用 CircleOptions 类围绕用户位置绘制圆圈。例如,假设有两部手机,我设置半径为 20 米。每当另一部手机进入 20 米半径内时,我想收到有人在我附近的通知。我将使用 firebase 进行实时位置获取。类似这样的问题很少,但其中没有一个有答案。任何帮助将不胜感激。

MainActivity.java

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;

import android.graphics.Color;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
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.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends FragmentActivity implements GoogleMap.OnMyLocationButtonClickListener,
GoogleMap.OnMyLocationClickListener, OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
LocationListener
{
GoogleMap mMap;
GoogleMapOptions options = new GoogleMapOptions();
GoogleApiClient googleApiClient;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);

buildClient();
options.mapType(GoogleMap.MAP_TYPE_SATELLITE)
.compassEnabled(false)
.rotateGesturesEnabled(false)
.tiltGesturesEnabled(false);
}
private void buildClient() {
googleApiClient = new GoogleApiClient.Builder(this.getApplicationContext())
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
@Override
public boolean onMyLocationButtonClick() {
Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
// Return false so that we don't consume the event and the default behavior still occurs
// (the camera animates to the user's current position).
return false;

}
@Override
public void onMyLocationClick(@NonNull Location location) {
Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
LocationRequest lr = new LocationRequest();
lr.setInterval(1000);
lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, lr, this);
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
Toast.makeText(this.getApplicationContext(), "Current Lat : " +currentLatitude + " Long : " + currentLongitude, Toast.LENGTH_SHORT).show();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(currentLatitude, currentLongitude))
.title("Marker"));
drawCircle(new LatLng(currentLatitude,currentLongitude));
}
private void drawCircle(LatLng point){
// Instantiating CircleOptions to draw a circle around the marker
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(point);
circleOptions.radius(20);
circleOptions.strokeColor(Color.BLACK);
circleOptions.fillColor(0x30ff0000);
circleOptions.strokeWidth(2);
mMap.addCircle(circleOptions);
}
@Override
public void onConnectionSuspended(int i) {

}
@Override
public void onLocationChanged(Location location) {
location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
LocationRequest lr = new LocationRequest();
lr.setInterval(1000);
lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, lr, this);
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
mMap.clear();
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(currentLatitude, currentLongitude)));
mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
drawCircle(new LatLng(currentLatitude,currentLongitude));
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

最佳答案

您可以使用地理围栏概念来检测第二部手机是否到达您的半径范围。地理围栏基本上是如何工作的?您有 2 部电话(考虑电话 A 和电话 B)。电话 A 的地理围栏半径为 20。当手机B进入手机A的20半径范围内时,地理围栏将被触发。

地理围栏 Android 开发者:https://developer.android.com/training/location/geofencing

您还可以使用第三方库进行地理围栏。我会向您推荐智能位置库。

GitHub:https://github.com/mrmans0n/smart-location-lib

关于java - 我希望当用户使用 GPS 进入 10 米半径时收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60901764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com