【问题标题】:Chat application to send location with firebase [closed]使用 Firebase 发送位置的聊天应用程序 [关闭]
【发布时间】:2017-05-03 15:08:45
【问题描述】:

如何在 android 中使用 firebase 发送我的位置坐标,我看到一个使用 firebase 的聊天应用程序,但我不明白如何将我的 Gps 代码放入其中可以像消息一样实时发送坐标

【问题讨论】:

    标签: android firebase location chat android-gps


    【解决方案1】:

    首先,您需要创建一个看起来像这样的模型类:

    public class MapModel {
        private String latitude;
        private String longitude;
    
        public MapModel() {}
    
        public MapModel(String latitude, String longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    
        public void setLatitude(String latitude) {this.latitude = latitude;}
        public String getLatitude() {return latitude;}
    
        public void setLongitude(String longitude) {this.longitude = longitude;}
        public String getLongitude() {return longitude;}
    }
    

    你需要像这样在Activity 中创建一个变量:

    private static final int PLACE_PICKER_REQUEST = 123;
    

    覆盖您的onActivityResult,它应该如下所示:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == PLACE_PICKER_REQUEST){
                if (resultCode == RESULT_OK) {
                    Place place = PlacePicker.getPlace(this, data);
                    if (place != null){
                        LatLng latLng = place.getLatLng();
                        MapModel mapModel = new MapModel(latLng.latitude + "", latLng.longitude + "");
                        databaseReference.push().setValue(mapModel);
                   }
                }
            }
        }
    

    要发送位置,请创建如下方法:

    private void locationPlacesIntent(){
        try {
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
            startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 你成功发送位置了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2017-09-30
    • 2011-04-21
    • 2011-04-10
    相关资源
    最近更新 更多