从Firebase检索位置并将标记放在google map api for android上

从Firebase检索位置并将标记放在google map api for android上,第1张

概述我正在尝试创建应用程序,以便在按下保存按钮时在firebase上存储位置,并从firebase检索位置并显示地图中的所有引脚.我已经能够将位置保存到具有纬度和经度的位置子项下的firebase,但我不知道如何获取值和引脚.我尝试按照firebase在手册上的方式进行 *** 作,但它没有用.有人可以帮忙解

我正在尝试创建应用程序,以便在按下保存按钮时在firebase上存储位置,并从firebase检索位置并显示地图中的所有引脚.我已经能够将位置保存到具有纬度和经度的位置子项下的firebase,但我不知道如何获取值和引脚.我尝试按照firebase在手册上的方式进行 *** 作,但它没有用.有人可以帮忙解决这个问题吗?

到目前为止这是我的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {private GoogleMap mMap;private final static int MY_PERMISSION_FINE_LOCATION = 101;private button mSavebutton;private DatabaseReference mDatabase;private DatabaseReference refDatabase;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_maps);// Obtain the SupportMapFragment and get notifIEd when the map is ready to be used.    SupportMapFragment mapFragment = (SupportMapFragment)     getSupportFragmentManager()            .findFragmentByID(R.ID.map);    mapFragment.getMapAsync(this);    mSavebutton = (button) findVIEwByID(R.ID.Savebtn);    mDatabase = FirebaseDatabase.getInstance().getReference().child("Navigation");    refDatabase = FirebaseDatabase.getInstance().getReference().child("Location"); }@OverrIDepublic voID onMapReady(GoogleMap GoogleMap) {    mMap = GoogleMap;    mMap.setonMapLongClickListener(new GoogleMap.OnMapLongClickListener() {        @OverrIDe        public voID onMapLongClick(LatLng point) {            // Todo auto-generated method stub            // added marker saved as marker and coordinates passed to latlng            Marker marker = mMap.addMarker(new            MarkerOptions().position(point));            final LatLng latlng = marker.getposition();            mSavebutton.setonClickListener(new VIEw.OnClickListener(){                @OverrIDe                public voID onClick(VIEw vIEw){                    DatabaseReference newPost = mDatabase.push();                    newPost.child("Location").setValue(latlng);                }            });          }       });       if (ActivityCompat.checkSelfPermission(this,     Manifest.permission.ACCESS_FINE_LOCATION) ==             PackageManager.PERMISSION_GRANTED) {        mMap.setMyLocationEnabled(true);    } else {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);        }    }}@OverrIDepublic voID onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {    super.onRequestPermissionsResult(requestCode, permissions, grantResults);    switch (requestCode) {        case MY_PERMISSION_FINE_LOCATION:            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==                        PackageManager.PERMISSION_GRANTED) {                    mMap.setMyLocationEnabled(true);                }            }else {                Toast.makeText(getApplicationContext(), "This requires location permissions to be granted", Toast                        .LENGTH_LONG).show();                finish();            }            break;    }  }}

这是位置在firebase中保存的方式

Data  -KIDp45TdInOM3Bsyu2b         Location         latitude:19.772613777905196          longitude:-9.92741011083126  -KIDsmTExZy2kjnS7S-b         Location         latitude: 18.221073689785065         longitude: -6.573890447616577  -KIDvmAgV0bm2uT_Pcdr         Location         latitude: 14.44608051870992         longitude: -6.510856859385967

解决方法:

首先:您需要将结构更改为:

 Data  Location    -KIDp45TdInOM3Bsyu2b       latitude:19.772613777905196        longitude:-9.92741011083126    -KIDsmTExZy2kjnS7S-b       latitude: 18.221073689785065       longitude: -6.573890447616577    -KIDvmAgV0bm2uT_Pcdr       latitude: 14.44608051870992       longitude: -6.510856859385967

这样做的方法是:

        mSavebutton.setonClickListener(new VIEw.OnClickListener(){            @OverrIDe            public voID onClick(VIEw vIEw){                //change to refDatabase or mDatabase.child("Location")                DatabaseReference newPost = refDatabase.push();                //the push() command is already creating unique key                newPost.setValue(latlng);            }        });

从refDatabase中将标记放入地图的代码是:

@OverrIDepublic voID onMapReady(GoogleMap GoogleMap) {   mMap = GoogleMap;   ...   refDatabase.addChildEventListener(new ChildEventListener() {     @OverrIDe     public voID onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {          LatLng newLocation = new LatLng(                        dataSnapshot.child("latitude").getValue(Long.class),                        dataSnapshot.child("longitude").getValue(Long.class)                    );          mMap.addMarker(new MarkerOptions()              .position(newLocation)              .Title(dataSnapshot.getKey()));     }     @OverrIDe     public voID onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}     @OverrIDe     public voID onChildRemoved(DataSnapshot dataSnapshot) {}     @OverrIDe     public voID onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}     @OverrIDe     public voID onCancelled(DatabaseError databaseError) {}  });}
总结

以上是内存溢出为你收集整理的从Firebase检索位置并将标记放在google map api for android上全部内容,希望文章能够帮你解决从Firebase检索位置并将标记放在google map api for android上所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097882.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存