我想在Googmap上使用4行3列。我想在网格制作的每个矩形的topLeft和右下角显示标记。
这是我正在使用的代码
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和right right points。从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)
{}
}
}
}
答案 0 :(得分:1)
您的代码似乎有一些关于坐标的错误。 西南是左下角,东北是右上角。 所以
我修改了内联代码,因此它可能包含错误,但它应该适用于您的情况。除了最后一行和最后一列之外,不要绘制低位标记,因为它会绘制一个重复的标记!
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 rightLng = mainMaxLng;
for (int i = 0; i < rows; i++)
{
for (int x = 0; x < columns ; x++)
{
double currentTopLat = (topLat - (i * verticalDiff));
double currentLeftLng = (topLng + (x * horizontalDiff));
try
{
MarkerOptions markerOptions = new MarkerOptions()
.draggable(false)
.position(new LatLng(Double.valueOf(currentTopLat), Double.valueOf(currentLeftLng)));
mMap.addMarker(markerOptions);
if ((i==(rows-1))|| (x==columns-1){//add the lowerright marker only on the last line or column, avoid duplicated markers
double currentBottomLat = currentTopLat - verticalDiff;
double currentRightLng = currentLeftLng + horizontalDiff;
markerOptions = new MarkerOptions()
.draggable(false)
.position(new LatLng(Double.valueOf(currentBottomLat), Double.valueOf(currentBottomLng)));
mMap.addMarker(markerOptions);
}
}catch (Exception e)
{}
}
}
}