2
我想要Googmap上的4行和3列的網格。我想在由網格製成的每個Rectangle的topLeft和右下角顯示標記。Android計算Android中的網格角位置Google地圖
這裏是我使用
public void getVisibleRegionGrids(final VisibleRegion region)
{
int columns = 3;
int rows = 4;
double mainTopLat = region.latLngBounds.southwest.latitude; // (c, d)
double mainTopLng = region.latLngBounds.northeast.longitude;
double mainBottomLat = region.latLngBounds.northeast.latitude;
double mainBottomLng = region.latLngBounds.southwest.longitude;
double horizontalDiff = Math.abs ((mainBottomLat - mainTopLat)/columns); // 1
double verticalDiff = Math.abs ((mainBottomLng - mainTopLng)/rows); // 1
double topLat = mainTopLat; // (c, d)
double topLng = mainTopLng;
//double bottomLat = mainTopLat + horizontalDiff;
//double bottomLng = mainTopLng + verticalDiff;
for (int i = 0; i < rows; i++)
{
for (int x = 0; x < columns ; x++)
{
double currentTopLat = (topLat + (i * verticalDiff)) + (x * horizontalDiff);
double currentTopLng = (topLng + (i * verticalDiff)) + (x * horizontalDiff);
double currentBottomLat = currentTopLat + horizontalDiff;
double currentBottomLng = currentTopLng + verticalDiff;
try
{
MarkerOptions markerOptions = new MarkerOptions()
.draggable(false)
.position(new LatLng(Double.valueOf(currentTopLat), Double.valueOf(currentTopLng))
);
mMap.addMarker(markerOptions);
markerOptions = new MarkerOptions()
.draggable(false)
.position(new LatLng(Double.valueOf(currentBottomLat), Double.valueOf(currentBottomLng))
);
mMap.addMarker(markerOptions);
}catch (Exception e)
{}
}
}
}
我無法得到它的邏輯工作的代碼。我計算了topLeft和右下角的分數。從topLeft開始,我想繼續。我能做些什麼
更新
感謝的幫助下,我設法使它工作
public void getVisibleRegionGrids(final VisibleRegion region)
{
int columns = 3;
int rows = 4;
double mainTopLat = region.latLngBounds.northeast.latitude; // (c, d)
double mainMaxLng = region.latLngBounds.northeast.longitude;
double mainBottomLat = region.latLngBounds.southwest.latitude;
double mainMinLng = region.latLngBounds.southwest.longitude;
double horizontalDiff = Math.abs ((mainMaxLng - mainMinLng)/columns); // 1
double verticalDiff = Math.abs ((mainTopLat - mainBottomLat)/rows); // 1
double topLat = mainTopLat; // (c, d)
double topLng = mainMinLng;
int position = 0;
for (int i = 0; i < rows; i++)
{
for (int x = 0; x < columns ; x++)
{
double currentTopLat = (topLat - (i * verticalDiff));
double currentLeftLng = (topLng + (x * horizontalDiff));
if(position == i)
{
currentTopLat = (topLat - (i * verticalDiff));
currentLeftLng = (topLng + (x * horizontalDiff));
}
position = -1;
if(x == columns)
position = i + 1;
try
{
MarkerOptions markerOptions = new MarkerOptions()
.draggable(false)
.position(new LatLng(Double.valueOf(currentTopLat), Double.valueOf(currentLeftLng)));
mMap.addMarker(markerOptions);
double currentBottomLat = currentTopLat - verticalDiff;
double currentRightLng = currentLeftLng + horizontalDiff;
markerOptions = new MarkerOptions()
.draggable(false)
.position(new LatLng(Double.valueOf(currentBottomLat), Double.valueOf(currentRightLng)));
mMap.addMarker(markerOptions);
}catch (Exception e)
{}
}
}
}
@N緯度是水平的嗎?所以應該考慮minLat和max lat以及topLng和bottomLng否?所以水平和垂直差異應該改變 –
nope,緯度是垂直的,從-90到90,經度是水平的,從-180到180. 你的想法是從左上角開始(topLat,minLng)並在網格中移動(雙循環掃描向右移動(列),當到達右側時,移至下一行 行用於減小緯度(從頂部開始),列用於增加經度(從minLng開始) 。 我編輯了代碼,因爲在double currentLeftLng =(topLng +(x * horizontalDiff))中出現了一個錯誤; –
@N好的謝謝你的幫助,我設法讓它工作。現在我有12個不同的包圍盒,再次感謝 –