简体   繁体   中英

Setting Google Map Markers and Assigning Bounds - Android

Here's the situation. I'm trying to create markers that only show up when they are 1 longitude and latitude away. I'm also trying to get the LatLngBounds to fit the markers within 1 latlng markers on the map from the outset.

I can get everything to work but not at the same time. I must doing something wrong with my If/Else statements.

protected void onPostExecute(Address address) {
        mMap.clear();
        if (address == null) {
            Toast.makeText(MainActivity.this, R.string.invalid_entry, Toast.LENGTH_LONG).show();
        } else {
            String addressName = "";
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                addressName += " - " + address.getAddressLine(i);
            }

            Double longitude =  address.getLongitude();
            Double latitude =  address.getLatitude();

            LatLng position = new LatLng(address.getLatitude(), address.getLongitude());

            LatLngBounds.Builder builder = new LatLngBounds.Builder();
            builder.include(position);
            builder.include(position);

            //search location
            mMap.addMarker(new MarkerOptions().position(position).title(addressName)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));

            for (int i = 0; i < markers.size(); i++) {
                Double distanceLng = Math.abs(markers.get(i).longitude - longitude);
                Double distanceLat = Math.abs(markers.get(i).latitude - latitude);
                if ((distanceLng < 1) && (distanceLat < 1)) {
                    mMap.addMarker(new MarkerOptions()
                            .position(markers.get(i))
                            .title("Place")
                            .snippet("Description"));
                    builder.include(markers.get(i));
                    LatLngBounds bounds = builder.build();
                    int padding = 50; // offset from edges of the map in pixels
                    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
                    Log.d(TAG, "DistanceBounds: " + bounds);
                    Log.d(TAG, "DistancePadding: " + padding);
                    Log.d(TAG, "DistanceI: " + i);
                    mMap.moveCamera(cu);
                }
                else {
                    Toast.makeText(MainActivity.this, R.string.no_places_in_area,Toast.LENGTH_LONG);
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 10));
                    Log.d(TAG, "DistanceLng Failed = " + distanceLng + "i = " + i);
                    Log.d(TAG, "DistanceLat Failed = " + distanceLat + "i = " + i);
                }
            }


        }

Any help with the writing the right code would be appreciated!

You are calling the Math.abs() method which returns the absolute value of given number ie Math.abs(-10) will return your 10. I would suggest you to use Math.floor or Math.ceil as per your requirement.

System.out.println(Math.ceil(25.46)); // Prints 26
System.out.println(Math.floor(25.46)); // Prints 25

this will give you the exact subtracted value. As the value of latitude and longitude are always in double so it might be possible the your will not get the exact value of 1 as per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM