Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何追踪朋友的位置?_Java_Android_Gps_Real Time - Fatal编程技术网

Java 如何追踪朋友的位置?

Java 如何追踪朋友的位置?,java,android,gps,real-time,Java,Android,Gps,Real Time,我是这个领域的新手,我正在尝试制作一个像“快照地图”这样的应用程序来跟踪我的朋友。我可以获得我的实时位置,但我不知道应该添加什么来获得使用同一应用程序的人的实时位置 这是我的Java代码,如果你们能给我一个代码,这对我真的很有帮助 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener { private FirebaseAuth mAuth; fina

我是这个领域的新手,我正在尝试制作一个像“快照地图”这样的应用程序来跟踪我的朋友。我可以获得我的实时位置,但我不知道应该添加什么来获得使用同一应用程序的人的实时位置

这是我的Java代码,如果你们能给我一个代码,这对我真的很有帮助

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

private FirebaseAuth mAuth;
final static int PERMISSION_ALL = 1;
final static String[] PERMISSIONS = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};

private GoogleMap mMap;
MarkerOptions mo;
Marker marker;
private Location user;
LocationManager locationManager;
private HashMap<Float, Location> otherUser = new HashMap<Float, Location>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    mAuth = FirebaseAuth.getInstance();

    // 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);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    mo = new MarkerOptions().position(new LatLng(0, 0)).title("My Current Location");
    if (Build.VERSION.SDK_INT >= 27 && !isPermissionGranted()) {
        requestPermissions(PERMISSIONS, PERMISSION_ALL);
    } else requestLocation();
    if (!isLocationEnabled())
        showAlert(1);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    marker = mMap.addMarker(mo);
    }

//Called when the location has changed.
@Override
public void onLocationChanged(Location location) {
    LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
    marker.setPosition(myCoordinates);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myCoordinates));

}
//Called when the provider status changes.
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

//Called when the provider is enabled by the user.
@Override
public void onProviderEnabled(String provider) {

}

//Called when the provider is disabled by the user. If requestLocationUpdates

@Override
public void onProviderDisabled(String provider) {

}

private void requestLocation() {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_HIGH);
    String provider = locationManager.getBestProvider(criteria, true);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(provider, 10000, 10, this);
}

private boolean isLocationEnabled() {
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
            locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

private boolean isPermissionGranted() {
    if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
            == PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v("mylog","Permission is granted");
        return true;
    }else{
        Log.v("mylog","Permission not granted");
        return false;

    }
}
private void showAlert(final int status) {
    String message, title, btnText;
    if (status == 1) {
        message = "Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
                "use this app";
        title = "Enable Location";
        btnText = "Location Settings";
    } else {
        message = "Please allow this app to access location!";
        title = "Permission access";
        btnText = "Grant";
    }
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setCancelable(false);
    dialog.setTitle(title)
            .setMessage(message)
            .setPositiveButton(btnText, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    if (status == 1) {
                        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(myIntent);
                    } else
                        requestPermissions(PERMISSIONS, PERMISSION_ALL);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    finish();
                }
            });
    dialog.show();
}
公共类MapsActivity扩展了FragmentActivity在MapReadyCallback、LocationListener上的实现{
私人消防队;
最终静态整数权限_ALL=1;
最终静态字符串[]权限={Manifest.permission.ACCESS\u rough\u LOCATION,Manifest.permission.ACCESS\u FINE\u LOCATION};
私有谷歌地图;
标记选项mo;
标记;
专用位置用户;
地点经理地点经理;
private HashMap otherUser=new HashMap();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
mAuth=FirebaseAuth.getInstance();
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
locationManager=(locationManager)getSystemService(位置服务);
mo=新标记选项().position(新LatLng(0,0)).title(“我的当前位置”);
如果(Build.VERSION.SDK_INT>=27&&!ispermissiongrated()){
请求权限(权限、权限\全部);
}else requestLocation();
如果(!isLocationEnabled())
显示警报(1);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
标记=mMap.addMarker(mo);
}
//当位置更改时调用。
@凌驾
已更改位置上的公共无效(位置){
LatLng坐标=新LatLng(location.getLatitude(),location.getLongitude());
标记器设置位置(支点);
mMap.moveCamera(CameraUpdateFactory.newLatLng(坐标));
}
//当提供程序状态更改时调用。
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
//当用户启用提供程序时调用。
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
//当用户禁用提供程序时调用。如果RequestLocationUpdate
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
私有void requestLocation(){
标准=新标准();
标准.设定准确度(标准.准确度/精细度);
标准。设置功率要求(标准。功率高);
字符串提供程序=locationManager.getBestProvider(条件为true);
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
locationManager.RequestLocationUpdate(提供者,10000,10,this);
}
私有布尔值isLocationEnabled(){
返回locationManager.isProviderEnabled(locationManager.GPS\U提供程序)||
locationManager.isProviderEnabled(locationManager.NETWORK\u提供程序);
}
私有布尔值isPermissionGrated(){
if(checkSelfPermission(Manifest.permission.ACCESS\u\u位置)
==PackageManager.PERMISSION| | checkSelfPermission(Manifest.PERMISSION.ACCESS|u FINE|u位置)
==PackageManager.权限(已授予){
Log.v(“mylog”,“已授予许可”);
返回true;
}否则{
Log.v(“mylog”,“未授予许可”);
返回false;
}
}
私有void showAlert(最终整数状态){
字符串消息、标题、btnText;
如果(状态==1){
message=“您的位置设置设置为“关闭”。\n请启用位置设置”+
“使用此应用程序”;
title=“启用位置”;
btnText=“位置设置”;
}否则{
message=“请允许此应用访问位置!”;
title=“权限访问”;
btnText=“格兰特”;
}
最终AlertDialog.Builder对话框=新建AlertDialog.Builder(此);
对话框。可设置可取消(false);
对话框.setTitle(标题)
.setMessage(消息)
.setPositiveButton(btnText,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface paraloginterface,int paramInt){
如果(状态==1){
Intent myIntent=新意图(设置、操作、位置、源、设置);
星触觉(myIntent);
}否则
请求权限(权限、权限\全部);
}
})
.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface paraloginterface,int paramInt){
完成();
}
});
dialog.show();
}

您应该使用维护良好的库,因为优化位置跟踪代码需要很多时间,因为电池耗尽。您必须将位置数据发送到某些服务器(如firebase),然后用新位置更新用户。是否意味着我必须在firebase中创建数据库?