为什么标记其他设备的位置没有出现在文件夹中

时间:2019-10-13 09:59:22

标签: android google-maps google-maps-markers

嘿,我只是Android的初学者。在保存纬度和纵向距离到Firebase之前,我尝试检索另一台设备的位置并将其显示在地图上。但是当我打开仅显示我的位置的地图时。其他设备位置没有出现,怎么解决?

放置当前用户纬度的按钮

            @Override
            public void onClick(View v) {

                fused.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        lat.setText(String.valueOf(location.getLatitude()));
                        lont.setText(String.valueOf(location.getLongitude()));

                        String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
                        Intent map = new Intent(MainActivity.this, trackingMaps.class);
                        map.putExtra("email", email);
                        map.putExtra("lat", location.getLatitude());
                        map.putExtra("lnt", location.getLongitude());
                        startActivity(map);
                    }
                });
            }
        });

这是获取经纬度的代码。并保存到Firebase

        fused.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                lat.setText(String.valueOf(location.getLatitude()));
                lont.setText(String.valueOf(location.getLongitude()));

                String latnya = lat.getText().toString().trim();
                String lntnya = lont.getText().toString().trim();
                String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
                String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

                tracking track = new tracking(email, lntnya, latnya, uid);
                locationsDatabase.child("locations").setValue(track);

            }
        });
    }

补习班


    String email;
    String lng;
    String lat;
    String uid;

    public tracking () {

    }

    public tracking(String email, String lng, String lat, String uid) {
        this.email = email;
        this.lng = lng;
        this.lat = lat;
        this.uid = uid;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getEmail() {
        return email;
    }

    public String getLng() {
        return lng;
    }

    public String getLat() {
        return lat;
    }

    public String getUid() {
        return uid;
    }
}

这是地图的代码。并添加标记

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tracking_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);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseUser = firebaseAuth.getCurrentUser();
        locationDatabase = FirebaseDatabase.getInstance().getReference(firebaseUser.getUid()).child("locations").child("email");

        if (getIntent() != null) {
            email = getIntent().getStringExtra("email");
            lat = getIntent().getDoubleExtra("lat", 0);
            lnt = getIntent().getDoubleExtra("lnt", 0);
        }

        if (!TextUtils.isEmpty(email)) {
            loadLocationForThisUser();
        }
    }

    private void loadLocationForThisUser() {
        Query userLocation = locationDatabase.orderByChild("email").equalTo(email);

        userLocation.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot posSnapshot:dataSnapshot.getChildren()) {
                    tracking track = posSnapshot.getValue(tracking.class);

                    //add marker friend location
                    LatLng friendLocation = new LatLng(Double.parseDouble(track.getLat()),
                            Double.parseDouble(track.getLng()));

                    //create location from user coordinater
                    Location curent = new Location("");
                    curent.setLatitude(lat);
                    curent.setLongitude(lnt);

                    //create location from friend coordiates
                    Location friend = new Location("");
                    friend.setLatitude(Double.parseDouble(track.getLat()));
                    friend.setLongitude(Double.parseDouble(track.getLng()));

                    //clear all old marks
                    mMap.clear();

                    //create function calculete distance between location (fungsi meghitung jarak)
                    distance(curent, friend);

                    //add friend marker in map
                    mMap.addMarker(new MarkerOptions().position(friendLocation).title(track.getEmail())
                    .snippet("Distance" + new DecimalFormat("#.#").format(distance(curent, friend)))
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lnt), 12.0f));
              }

                //create marker for user
                LatLng currentlatlng = new LatLng(lat, lnt);
                mMap.addMarker(new MarkerOptions().position(currentlatlng).title(FirebaseAuth.getInstance().getCurrentUser()
                .getEmail()));
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lnt), 12.0f));
            }

没有出现错误,但标记了其他设备未出现的位置。必须纠正什么? 我真的希望你们能帮助我。谢谢!

0 个答案:

没有答案