簡體   English   中英

使用 android Geofire 獲取經度和緯度

[英]Get longitude and latitude using android Geofire

我正在嘗試使用 Geo fire for android 獲取用戶位置。 我試圖獲取用戶的經度和緯度並將其保存在 firebase 數據庫中,但我沒有運氣。

下面是我的代碼。 有人能告訴我我做錯了什么嗎?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_after_registration);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

    savebutton=findViewById(R.id.save);

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef =  FirebaseDatabase.getInstance().getReference();
    DatabaseReference update = rootRef.child("Users").child(uid);

    GeoFire geoFire=new GeoFire(rootRef);

    geoFire.getLocation("location", new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation location) {
            if(location!=null){

                Double longi = location.longitude;
                Double lat = location.latitude;

                update.child("longitude").setValue(longi);
                update.child("latitude").setValue(lat);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

在 GeoFire 中,您可以通過字符串鍵設置和查詢位置。 要為鍵設置位置,只需調用 setLocation 方法:

geoFire.setLocation("location", new GeoLocation(37.7853889, -122.4056973), new GeoFire.CompletionListener() {
    @Override
    public void onComplete(String key, FirebaseError error) {
        if (error != null) {
            System.err.println("There was an error saving the location to GeoFire: " + error);
        } else {
            System.out.println("Location saved on server successfully!");
        }
    }
});

設置位置后,您可以檢索它並將值保存到數據庫:

geoFire.getLocation("location", 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))
// save longitude and latitude to db
            Double longi = location.longitude;
            Double lat = location.latitude
        } 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);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM