如何在应用程序的地图上显示动画驾驶汽车

Posted

技术标签:

【中文标题】如何在应用程序的地图上显示动画驾驶汽车【英文标题】:How to display animated driver cars on the map in an app 【发布时间】:2018-08-26 00:00:24 【问题描述】:

我的数据库

在上面的树中,我存储了驱动程序可用的当前纬度和经度。如果在 driversAvailable 树下存在多个驱动程序,那么如何在地图上显示所有驱动程序?到目前为止,我已经编写了以下代码,但它无法正常工作:

private DatabaseReference driverLoadLocationRef;
private ValueEventListener driverLoadLocationRefListener;
private void loadAllAvailableDrivers() 
    DatabaseReference driverLoading = FirebaseDatabase.getInstance().getReference().child("driversAvailable");
    GeoFire geoDriver = new GeoFire(driverLoading);
    GeoQuery geoQueryDriver = geoDriver.queryAtLocation(new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()),distance);
    geoQueryDriver.removeAllListeners();
    geoQueryDriver.addGeoQueryEventListener(new GeoQueryEventListener() 
        @Override
        public void onKeyEntered(final String key, GeoLocation location) 
            if (!driverLoad ) 
                driverLoad = true;
                driverLoadId = key;
                driverLoadLocationRef = FirebaseDatabase.getInstance().getReference().child("driversAvailable").child(driverFoundId).child("l");
                driverLoadLocationRefListener = driverLoadLocationRef.addValueEventListener(new ValueEventListener() 
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) 
                        if (dataSnapshot.exists() ) //If doesnot exist check then app will crash
                        
                            List<object> map = (List<object>) dataSnapshot.getValue();
                            double locationLoadLat = 0;
                            double locationLoadLng = 0;
                            if (map.get(0) != null)
                            
                                locationLoadLat = Double.parseDouble(map.get(0).toString());
                            
                            if (map.get(1) != null)
                            
                                locationLoadLng = Double.parseDouble(map.get(1).toString());
                            
                            LatLng driverLoadLatLng = new LatLng(locationLoadLat,locationLoadLng);
                            if (mDriverMarker != null) //If not added app crash since it will try to remove which doesnot exist
                            
                                mDriverMarker.remove();
                            
                            mDriverMarker = mMap.addMarker(new MarkerOptions().position(driverLoadLatLng).flat(true)
                                    .title(driverLoadId)
                                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.car3)));
                        
                    
                    @Override
                    public void onCancelled(DatabaseError databaseError) 
                        Toast.makeText(PassengerMapsActivity.this,"Failed",Toast.LENGTH_SHORT).show();
                    
                );
            
        
        @Override
        public void onKeyExited(String key) 
        
        @Override
        public void onKeyMoved(String key, GeoLocation location) 
        
        @Override
        public void onGeoQueryReady() 
            if (distance <= LIMIT)
            
                distance++;
                loadAllAvailableDrivers();
            
        
        @Override
        public void onGeoQueryError(DatabaseError error) 
        
    );

【问题讨论】:

我认为您没有正确加载 .position 处的纬度和经度 先生,您能告诉我代码部分的问题出在哪里,因为经纬度位置在数据库中,但我无法在地图上表示。 您似乎没有正确使用 GeoFire。将为满足查询的每个驱动程序调用“onKeyEntered”方法-“位置”参数是该驱动程序的位置,因此无需重新查询。在您的示例中,“driversAvailble”节点树是由 Geofire 管理的区域,您通常不会直接访问它 - 但显然您 可以 因为它是可访问的。 【参考方案1】:

我认为我有更简单的实现方式!

创建一个Marker数组,用onDataChange的条目数(childrenCount)初始化数组。 之后,在 'for(DataSnapshot ds : dataSnapshot.getChildren())' 中嵌入另一个 for..loop,迭代找到的条目数(childrenCount)。

我假设您有一个用于将数据推送到 firebase 的 Model 类。使用它来检索特定的坐标。 Av 决定将您的 '0' 和 '1' 替换为 'lat' 和 'lon'。

private void loadAllAvailableDrivers() 
DatabaseReference driverLoading = FirebaseDatabase.getInstance().getReference("driversAvailable");
driverLoading.addValueEventListener(new ValueEventListener() 
    @Override
        public void onDataChange(DataSnapshot dataSnapshot) 
            //trying to get several markers
            //Create an array and specify a size which will be initialized to children(driver) count
            int size = (int) dataSnapshot.getChildrenCount(); //
            Marker[] allMarkers = new Marker[size];
            map.clear();
            for(DataSnapshot ds : dataSnapshot.getChildren()) 
               //Specify your model class here
                ModelClass modelObject=new ModelClass();
                //lets create a loop
                 for(int i=0;i<=size;i++) 
                    try 
                        //am assuming the model class uses variable 'lat',lon,name
                         modelObject.setLat(ds.getValue(ModelClass.class).getLat());
                         modelObject.setLon(ds.getValue(ModelClass.class).getLon());
                         modelObject.setName(ds.getValue(ModelClass.class).getName());

                        //lets retrieve the coordinates and store them in a variable
                        String myName=modelObject.getName();
                        Double latitude = Double.parseDouble(modelObject.getLat());
                        Double longitude = Double.parseDouble(modelObject.getLon());

                        //convert the coordinates to LatLng
                        LatLng latLng = new LatLng(latitude, longitude);
                        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                        //Now lets add updated markers
                        //lets add updated marker
                     allMarkers[i] = map.addMarker(new MarkerOptions()
                             .icon(BitmapDescriptorFactory.fromResource(R.drawable.car3)).position(latLng).title(myName));
                 catch (Exception ex)
                 


           
        

        @Override
        public void onCancelled(DatabaseError databaseError) 
            Toast.makeText(PassengerMapsActivity.this,"Failed",Toast.LENGTH_SHORT).show();

        
    );
 

【讨论】:

以上是关于如何在应用程序的地图上显示动画驾驶汽车的主要内容,如果未能解决你的问题,请参考以下文章

自动驾驶汽车是如何利用高精度地图和高精度定位来进行“导航”的

智能网联汽车 自动驾驶地图数据质量规范

智能网联汽车 自动驾驶地图数据质量规范

云计算未来可以满足自动驾驶汽车的需求吗?

助力自动驾驶商业化 高德公布高精地图技术路线图

助力自动驾驶商业化 高德公布高精地图技术路线图