0

大家好,我对应用程序开发非常陌生,有什么想法可以存储我的用户位置的纬度和经度及其名称并在我的谷歌地图中绘制吗?

我在下面使用了这段代码,它工作正常,它按预期绘制了多个标记,我的问题是,我的数据来自 firebase 数据库,请看图片在此处输入图像描述

2 个职位的用户:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private GoogleMap googleMap;
    private MarkerOptions options = new MarkerOptions();
    private ArrayList<LatLng> latlngs = new ArrayList<>();
    private ArrayList<String> Names = new ArrayList<>();







    @Override
    protected 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);




        latlngs.add(new LatLng(12.334343, 33.43434)); //some latitude and logitude value
        latlngs.add(new LatLng(10.334343, 32.43434)); //some latitude and logitude value
        Names.add("Name 1"); //some latitude and logitude value
        Names.add("Name 2"); //some latitude and logitude value







}


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
      //  LatLng sydney = new LatLng(-34, 151);
       // mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
      //  mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));


        for (LatLng point : latlngs) {
            options.position(point);
            options.title(String.valueOf(Names));
            options.snippet("someDesc");
            googleMap.addMarker(options);
        }



    }
}

4

2 回答 2

2

下面的代码用于从 firebease 获取数据:

       FirebaseDatabase database = FirebaseDatabase.getInstance();

       DatabaseReference myRef = database.getReference("Positions");

       myRef.addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(DataSnapshot dataSnapshot) {

               for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
                   System.out.println("id",""+ chidSnap.getKey()); //displays the key for the node
                   System.out.println("Latit",""+ chidSnap.child("Latitude").getValue());  
                   System.out.println.v("Longitude",""+ chidSnap.child("Longitude").getValue());   //gives the value for given keyname
               }

           }

           @Override
           public void onCancelled(DatabaseError databaseError) {

           }
       });
   }

于 2020-01-29T05:13:56.937 回答
1

您从 firbase 数据库中获取数据并将其存储在要在地图上发布标记的列表中

你可以使用这个方法

public ArrayList<MarkerOptions> publishMarker(GoogleMap googleMap, int recourceMarker) {
    ArrayList<MarkerOptions> markerOption = new ArrayList<>();
    for (LatLng point : latlngs) {
        MarkerOptions markerOptions = new MarkerOptions().position(point);

  markerOptions.icon(BitmapDescriptorFactory.fromResource(recourceMarker));
        markerOption.add(markerOptions);
        googleMap.addMarker(markerOptions).setTag(point);
        googleMap.addMarker(markerOptions);
    }
    return markerOption;
}

然后在 mapReady 方法中调用方法

ArrayList<MarkerOptions> markerOptions=publishMarker(mMap, R.drawable.map_marker);
 if (markerOptions!=null&&markerOptions.size() > 0)this.mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(markerOptions.get(0).getPosition(), 15));
于 2020-01-29T08:41:56.837 回答