我想在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和右下角的点。从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)
{}
}
}
}
最佳答案
您的代码似乎有一些关于坐标的错误。
西南是左下角,东北是右上角。
所以
mainTopLat是northeast.latitude
mainTopLng是northeast.longitude->应称为mainMaxLng
mainBottomLat位于西南。
mainBottomLng是southwest.longitude->应称为mainMinLng
我正在内联修改代码,因此可能包含错误,但它应适合您的情况。除最后一行和最后一列外,请勿绘制右下标记,因为它会绘制重复的标记!
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)
{}
}
}
}
关于java - Android计算Android Google map 中的网格角位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31042396/