以下是这种情况。我试图创建仅在经度和纬度为1时才显示的标记。我还试图让LatLngBounds从一开始就在地图上的1个latlng标记内拟合标记。
我可以让一切工作但不能同时工作。我必须对我的If / Else语句做错。
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);
}
}
}
任何有关编写正确代码的帮助都将不胜感激!
答案 0 :(得分:0)
您正在调用Math.abs()方法,该方法返回给定数字的绝对值,即Math.abs(-10)将返回您的10.我建议您使用Math.floor或Math.ceil作为您的要求。
System.out.println(Math.ceil(25.46)); // Prints 26
System.out.println(Math.floor(25.46)); // Prints 25
这将为您提供确切的减去值。由于纬度和经度的值总是加倍,因此您可能无法根据您的要求获得1的确切值。