如何制作一个按钮来查找我手动放置在 Google 地图上的最近的标记?

How to make a button to find the nearest marker I manually put on Google Maps?

所以我正在努力如何使这个 button "btnFindCybercafe" 工作。我只想让这个 button 显示离当前位置最近的 marker,大约 10 公里

下面是我的地图java代码

public class mapCybercafe extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    protected GoogleMap LokasiUser;
    private static final int LOCATION_REQUEST = 500;
    protected Marker LokasiCybercafe;

    final LatLng NOBLENATION = new LatLng(3.556744, 98.677177 );
    final LatLng H2NET       = new LatLng(3.534811, 98.591373 );
    final LatLng BRITANNIA   = new LatLng(3.572011, 98.698239 );
    final LatLng ICAFEMEDAN  = new LatLng(3.598079, 98.663644 );
    final LatLng MILALAMAS   = new LatLng(3.592356, 98.685277 );
    final LatLng CYPRESSNET  = new LatLng(3.555556, 98.633377 );
    final LatLng LASVEGASNET = new LatLng(3.572083, 98.694061 );

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_map);
        // Obtain the SupportMapFragment
        final SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        mMap.clear();


        Button btnAbout = findViewById(R.id.btn_about);
        Button btnHelp = findViewById(R.id.btn_help);
        Button btnFindCybercafe = findViewById(R.id.btn_FindCybercafe);


        btnAbout.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Intent intentAbout = new Intent(mapCybercafe.this, AboutActivity.class);
                                            startActivity(intentAbout);
                                        }
                                    }

        );
        btnHelp.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           Intent intentAbout = new Intent(mapCybercafe.this, HelpActivity.class);
                                           startActivity(intentAbout);
                                       }
                                   }

        );
        btnFindCybercafe.setOnClickListener( new View.OnClickListener(){
                                                 @Override
                                                 public void onClick(View v) {
                                                     do {

                                                     }
                                                 }
                                             }
        );
    }

    /**
     * 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.
     * 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;
        LokasiUser = googleMap;
        // realtime user location
        LokasiUser.getUiSettings().setZoomControlsEnabled(true);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
                (this, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST);
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;

        }
        LokasiUser.setMyLocationEnabled(true);

        // Add a marker of cybercafe in layout
        LokasiCybercafe = googleMap.addMarker(new MarkerOptions()
        .position(LASVEGASNET).title("Las Vegas Net CYBERCAFE").snippet("i5, 8GB RAM, Nvidia GEFORCE 1050"));

        LokasiCybercafe = googleMap.addMarker(new MarkerOptions()
                .position(NOBLENATION).title("NOBLENATION CYBERCAFE").snippet("i5, 16GB RAM, Nvidia GEFORCE 1050Ti"));

        LokasiCybercafe = googleMap.addMarker(new MarkerOptions()
                .position(H2NET).title("H2-NET CYBERCAFE").snippet("AMD RYZEN, 8GB RAM, Nvidia GEFORCE 1020Ti"));

        LokasiCybercafe = googleMap.addMarker(new MarkerOptions()
                .position(BRITANNIA).title("BRITANNIA-NET CYBERCAFE").snippet("i3, 8GB RAM, Nvidia GEFORCE 980Ti"));

        LokasiCybercafe = googleMap.addMarker(new MarkerOptions()
                .position(ICAFEMEDAN).title("iCAFE-MEDAN ").snippet("i3, 4GB RAM, Nvidia GEFORCE 980"));

        LokasiCybercafe = googleMap.addMarker(new MarkerOptions()
                .position(MILALAMAS).title("MILALAMAS-NET CYBERCAFE").snippet("i5, 8GB RAM, Nvidia GEFORCE 1060Ti"));

        LokasiCybercafe = googleMap.addMarker(new MarkerOptions()
                .position(CYPRESSNET).title("CYPRESS-NET CYBERCAFE").snippet("i3, 4GB RAM, Nvidia GEFORCE 970"));

    }
    @SuppressLint("MissingPermission")
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case LOCATION_REQUEST:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    mMap.setMyLocationEnabled(true);
                }
                break;
        }
    }
}

我真的需要有人帮助我,因为我的截止日期是明天。

首先将您所有的位置存储到一个arrayList 中。让我们称之为 locationList。因此,在您的 locationList 中,您拥有放置标记的所有位置。

现在假设您获得了当前位置并将其存储在变量名中 当前位置。

现在你必须找出最近的标记。为此,您需要计算每个位置与 currentLocation 之间的距离。所以首先创建一个 return 以米为单位的距离的方法。这是距离的代码。

private double getDistanceInMeter(LatLng start, LatLng end) {
    Location startPoint=new Location("locationA");
    startPoint.setLatitude(start.latitude);
    startPoint.setLongitude(start.longitude);

    Location endPoint=new Location("locationB");
    endPoint.setLatitude(end.latitude);
    endPoint.setLongitude(end.longitude);

    double distance=startPoint.distanceTo(endPoint);

    return distance;
}

现在考虑两个变量,一个用于距离跟踪,另一个用于具有最小距离的位置的索引。

现在我们要运行一个循环来计算每个距离

double distance = 0;
 int nearestLocationIndex = 0;
    for(int i = 0; i<locationList.size(); i++) {
        double currentDistance = getDistanceInMeter(locationList.get(i), currentLocation);

        if(i == 0 ) { // Initially we are assuming first position is the nearest
            distance = currentDistance;
            nearestLocationIndex = i;
        } else {
            if(currentDistance < distance) {
                distance = currentDistance;
                nearestLocationIndex = i;
            }
        }

    }

现在你知道哪个位置最近了,因为你有那个位置的索引。

LatLng nearestLocation = locationList(nearestLocationIndex);

我希望你知道如何实现它。