java - 如何在 map 上显示折线

标签 java android firebase firebase-realtime-database

我尝试显示折线,当我将其放入 onLocationResult 时,它显示了正确的线。 。

但是,我只希望它在用户单击开始按钮时显示折线。 所以我尝试将代码放入 onClickListener ,屏幕只显示标记,不显示线条。

Location mLastLocation;
    LocationRequest mLocationRequest;
    private SupportMapFragment mapFragment;
    private FusedLocationProviderClient mFusedLocationClient;
    private FirebaseAuth myAuth;
    private FirebaseDatabase mDatabase;
    private DatabaseReference myRef;
    private Marker currentUserLocationMarker;
    private ArrayList<LatLng> points; //added
    Polyline line;
    private Button btnStartRun;
    LatLng latLng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_googls_maps);
        // 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);

        points = new ArrayList<LatLng>();

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        btnStartRun=findViewById(R.id.btnStartRun);

        btnStartRun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                points.add(latLng);//add points to the array
                redrawLine();

                myAuth=FirebaseAuth.getInstance();
                DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("OnlineUsers");//.child("OnlineUser");
                DatabaseReference currentUserDB=mDatabase.child(myAuth.getCurrentUser().getUid());
                currentUserDB.child("CurrentLatitude").setValue(mLastLocation.getLatitude());
                currentUserDB.child("CurrentLongitude").setValue(mLastLocation.getLongitude());
            }
        }); 
    }

这是我的onLocationResult

 LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {
                mLastLocation = location;

                latLng = new LatLng(location.getLatitude(), location.getLongitude());

                mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(15));


            }
        }
    };

这是我的redrawline()

    private void redrawLine(){

        mMap.clear();  //clears all Markers and Polylines

        PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);//set the colour and width of the polyline
        for (int i = 0; i < points.size(); i++) {
            LatLng point = points.get(i);
            options.add(point);
        }
        addMarker(); //add Marker in current position
        line = mMap.addPolyline(options); //add Polyline
    }

谢谢。

最佳答案

您仅在单击按钮时添加点,但这是不正确的。每次收到 onLocationChanged (或者可能是您的位置回调)时,您都应该添加点,否则,您将无法绘制任何线条。

您的 onLocationChanged (或回调)每次都应该调用 redrawLine 方法。

你的redrawLine方法应该有一个标志,例如hasStarted。如果 hasStarted = false,则清除 map ,但不绘制线条。如果 hasStarted = true,则清除 map 并绘制线条。

在 onClick 监听器中,只需设置 hasStarted = true (或者,如果您希望它是一个切换,请设置 hasStarted = !hasStarted)。

关于java - 如何在 map 上显示折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60304398/

相关文章:

java - 调用textField.getText()时出现空指针异常

c# - 无法启动应用程序, "transparent"错误

javascript - 异步调用 firebase 后,Angular 应用程序未更新(脏检查)

firebase - 带有 Firebase Auth 的 Flutter Web 无法在本地工作,但在部署在云中时可以工作

java - 字符串相等,但 if 语句不触发

java - PHP 中的健身函数

android - 重用从 Realm 数据库中删除的对象

android - 如何在 Kotlin/Arrow.kt 中为生产者 channel 实现 "Railway Pattern"

Firebase 云函数 "admin.messaging(...).send is not a function"

eclipse - 全新安装的 Eclipse Mars 无法运行