在Android中使用Google Map API添加静态正方形网格 [英] Add static square grids using google map api in android
问题描述
在我的项目中,我需要在Google Map中定义区域.因此,我想到了使用google-maps-API在Android的静态正方形网格中划分google map.我是google-maps-API的新手,所以任何帮助都会很棒.
In my project, I need to define areas in google map. So, I thought of dividing google map in static square grids in android using google-maps-API. I am new to google-maps-API so any help would be great.
推荐答案
之前曾问过类似的问题,@MaciejGórski提供了一个不错的解决方案:
The similar question was asked before and @MaciejGórski provided a nice solution:
https://stackoverflow.com/a/16359857/5140781
我可以添加一些注释,因为前面提到的答案已经很旧了,链接不再有效.他们引用几年前已弃用的Google代码中的项目.我搜索并发现该项目已从Google代码移至GitHub:
There are several notes I can add because the aforementioned answer is quite old and links are not valid anymore. They refer to the project in Google code that was deprecated a couple of years ago. I searched and figured out that the project moved from Google code to GitHub:
https://github.com/mg6maciej/android-maps-extensions
因此,您可以按照@MaciejGórski的说明进行操作,并从以下URL复制文件DebugHelper.java
和SphericalMercator.java
So you can follow instructions of @MaciejGórski and copy files DebugHelper.java
and SphericalMercator.java
from the following URLs
还请注意,Google Maps Android API中的onCameraChange
已被弃用,因此您必须改用onCameraIdle
.
Also note that onCameraChange
in Google Maps Android API was deprecated, so you have to use onCameraIdle
instead.
我创建了一个示例项目,并能够创建网格
I created a sample project and was able to create a grid
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnCameraIdleListener {
private GoogleMap mMap;
private DebugHelper hlp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng center = new LatLng(41.385064,2.173403);
mMap.getUiSettings().setZoomControlsEnabled(true);
hlp = new DebugHelper();
mMap.setOnCameraIdleListener(this);
mMap.moveCamera(CameraUpdateFactory.newLatLng(center));
}
@Override
public void onCameraIdle() {
Projection projection = mMap.getProjection();
double l1 = projection.getVisibleRegion().farLeft.longitude;
double l2 = projection.getVisibleRegion().farRight.longitude;
double grdSize = Math.abs(l2-l1) / 8.0;
hlp.drawDebugGrid(mMap, grdSize);
}
}
您可以在GitHub上找到完整的示例项目
You can find a complete sample project on GitHub
https://github.com/xomena-so/so48834248
请在google_maps_api.xml
中用您的API密钥替换我的API密钥.
Please replace my API key with yours in google_maps_api.xml
.
我希望这会有所帮助!
这篇关于在Android中使用Google Map API添加静态正方形网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!