首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何制作一个按钮,找到最近的标记,我手动放在谷歌地图上?

如何制作一个按钮,找到最近的标记,我手动放在谷歌地图上?
EN

Stack Overflow用户
提问于 2018-08-24 11:56:53
回答 1查看 240关注 0票数 0

所以我很难让这个button "btnFindCybercafe“起作用。我只想让这个button显示出离目前位置10公里的距离的最近的marker

下面的是我的地图java代码

代码语言:javascript
复制
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;
        }
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-24 12:25:18

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

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

现在你必须找出最近的标记。要做到这一点,您需要计算每个位置到currentLocation的距离。因此,首先创建一个以米为单位返回距离的方法。这是距离的代码。

代码语言:javascript
复制
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;
}

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

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

代码语言:javascript
复制
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;
            }
        }

    }

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

代码语言:javascript
复制
LatLng nearestLocation = locationList(nearestLocationIndex);

我希望你对如何实现这一点有一个想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52003982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档