首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Google Map中使用坐标绘制所有Childnodes

在Google Map中使用坐标绘制所有Childnodes
EN

Stack Overflow用户
提问于 2017-01-13 00:57:26
回答 2查看 110关注 0票数 0

我的Capstone项目需要帮助。我知道一些从Google Firebase检索数据的基础知识,但我被一个问题卡住了。我的Firebase结构如下所示:

代码语言:javascript
复制
BusNumber {
  9009 {
      Location: "10.134342,  124.8380294"
  }
  9010 {
      Location: "10.248606, 124.750047"
  } 
  9011 {
      Location: "10.035522, 124.982952"
  }
}

我想检索所有的坐标,并在谷歌地图上绘制它们。谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-13 02:18:03

假设"BusNumber“是您的RootNode (否则您必须在调用addChildEventListener之前调整初始引用)..

代码语言:javascript
复制
@Override
public void onMapReady(GoogleMap googleMap) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    reference.addChildEventListener(new ChildEventListener(){
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
            String title = dataSnapshot.getKey();
            LatLng position = getLatLngFromString(dataSnapshot.child("Location").getValue(String.class));
            googleMap.addMarker(new MarkerOptions()
                    .position(position)
                    .title(title));
        }
        /* Other Overriden Methods Omitted */
    });
}


public static LatLng getLatLngFromString(String s) {
    String[] a = s.split("\\s*,\\s*");
    if (a.length > 2 || a[0].isEmpty() || a[1].isEmpty()) {
        throw new IllegalArgumentException("LatLng String is not valid!");
    }
    return new LatLng(Double.parseDouble(a[0]), Double.parseDouble(a[1]));
}

这应该可以让你开始学习了。显然,您需要注册/注销孩子事件侦听器,处理childeventlistener中的其他方法等。

我假设您已经设置了GoogleMap,如果您还没有,请参考官方指南:https://developers.google.com/maps/documentation/android-api/map-with-marker

票数 0
EN

Stack Overflow用户

发布于 2017-01-13 03:47:58

您应该尝试使用https://github.com/firebase/geofire-java来处理GeoLocation数据。我认为这是按位置等查询数据的最简单方法。

基本上,您将获得Firebase查询的结果,并使用以下代码:

代码语言:javascript
复制
geoFire.getLocation("firebase-hq", new LocationCallback() {
    @Override
    public void onLocationResult(String key, GeoLocation location) {
        if (location != null) {
            System.out.println(String.format("The location for key %s is [%f,%f]", key, location.latitude, location.longitude));
        } else {
            System.out.println(String.format("There is no location for key %s in GeoFire", key));
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        System.err.println("There was an error getting the GeoFire location: " + databaseError);
    }
});

希望能对你有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41618857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档