Android Google Maps - 标记颜色编码仅适用于最后的Firestore结果
创始人
2024-08-14 00:00:11
0

要将标记颜色编码应用于最后的Firestore结果,可以使用以下代码示例:

首先,确保已在项目中添加Google Maps和Firestore的依赖项。

// 引入必要的包
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

// 在活动或片段中实现OnMapReadyCallback接口
public class YourActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private FirebaseFirestore db;

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

        // 获取Firestore实例
        db = FirebaseFirestore.getInstance();

        // 获取SupportMapFragment
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // 查询Firestore数据
        db.collection("your_collection")
                .get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        // 遍历查询结果
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            // 获取位置坐标
                            double latitude = document.getDouble("latitude");
                            double longitude = document.getDouble("longitude");
                            LatLng location = new LatLng(latitude, longitude);

                            // 获取标记颜色编码
                            float color = document.getLong("color").floatValue();

                            // 在地图上添加标记
                            MarkerOptions markerOptions = new MarkerOptions()
                                    .position(location)
                                    .icon(BitmapDescriptorFactory.defaultMarker(color));
                            Marker marker = mMap.addMarker(markerOptions);
                        }
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                });
    }
}

确保替换代码中的以下部分:

  • your_collection:Firestore中的集合名称。
  • latitudelongitude:Firestore文档中包含位置坐标的字段名称。
  • color:Firestore文档中包含标记颜色编码的字段名称。

此代码将在地图上显示从Firestore获取的位置,并根据每个文档中的标记颜色编码设置标记的颜色。

相关内容

热门资讯

透视挂!wpk透视脚本,hhp... 透视挂!wpk透视脚本,hhpoker免费辅助器,总结教程(有挂工具)1、wpk透视脚本透视辅助简单...
透视透视"aapok... 透视透视"aapoker真的假的"详细辅助安装教程(辅助透视技巧)1、许多玩家不知道aapoker真...
透视系统!智星德州菠萝插件官网... 透视系统!智星德州菠萝插件官网,wepoker的辅助器,教你攻略(有挂细节)该软件可以轻松地帮助玩家...
透视安装"hhpok... 这是一款非常优秀的hhpoker是正规的吗 ia辅助检测软件,能够让你了解到hhpoker是正规的吗...
透视了解!aapoker辅助挂... 透视了解!aapoker辅助挂,wepoker究竟有没有透视,线上教程(有挂脚本);1、aapoke...
透视有挂"epoke... 透视有挂"epoker底牌透视"详细辅助新版2025教程(辅助软件方法)1、很好的工具软件,可以解锁...
透视挂!hhpoker透视脚本... 透视挂!hhpoker透视脚本视频,wepoker数据分析工具,技巧教程(有挂黑科技)1、让任何用户...
透视神器"德州局怎么... 此外,数据分析德州(德州局怎么透视)辅助神器app还具备辅助透视行为开挂功能,通过对客户德州局怎么透...
透视挂!德普之星辅助功能如何设... 透视挂!德普之星辅助功能如何设置,德普之星辅助器,AI教程(有挂插件);1、下载好德普之星辅助功能如...
透视透视"wepok... 透视透视"wepoker有脚本吗"详细辅助爆料教程(辅助器下载软件)1、很好的工具软件,可以解锁游戏...