在Android版Google地图中突出显示整个国家 [英] Highlight whole countries in Google Maps for Android
问题描述
引用:
我正在尝试由用户在Google Maps Android应用程序中返回所选国家/地区的位置.
I'm trying to return the location of selected countrys by the user in the Google Maps Android application.
类似的东西,但在Android谷歌地图中.有可能在android中做到这一点吗?
Something like this but in Android google maps. Is there a possibility to do this in android?
是否可以离线进行此操作?
Is it possible to do this offline?
推荐答案
关于Google Maps API中的国家或其他要素边框的问题已被多次问到,但是不幸的是Google并未公开公开此数据.
The question about country or other features borders in Google Maps APIs was asked many times, however, unfortunately Google doesn't expose this data publicly.
要突出显示国家/地区,您应将自己的数据用作Google Maps图层.在以下答案中提出了不错的解决方案
To highlight the country you should apply your own data as the Google Maps layer. Nice work around was proposed in the following answer
https://stackoverflow.com/a/40172098/5140781
您可以从OSM下载GeoJSON格式的国家边界.之后,您可以使用Google Maps Android API 实用程序库添加在应用程序中 GeoJsonLayer .
You can download country borders in GeoJSON format from OSM. After that you can use the Google Maps Android API Utility Library to add GeoJsonLayer in your application.
在我的示例中,我以GeoJSON格式下载了西班牙的边界,并使用GeoJsonLayer加载了GeoJSON文件.
For my example I downloaded the boundaries of Spain in GeoJSON format and loaded the GeoJSON file using GeoJsonLayer.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@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;
mMap.getUiSettings().setZoomControlsEnabled(true);
LatLng madrid = new LatLng(40.416775,-3.70379);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(madrid, 3F));
try {
GeoJsonLayer layer = new GeoJsonLayer(mMap, R.raw.es_geojson, getApplicationContext());
GeoJsonPolygonStyle style = layer.getDefaultPolygonStyle();
style.setFillColor(Color.MAGENTA);
style.setStrokeColor(Color.MAGENTA);
style.setStrokeWidth(1F);
layer.addLayerToMap();
} catch (IOException ex) {
Log.e("IOException", ex.getLocalizedMessage());
} catch (JSONException ex) {
Log.e("JSONException", ex.getLocalizedMessage());
}
}
}
您可以从github下载完整的示例项目,别忘了更改values/google_maps_api.xml中的API密钥
You can download the complete sample project from github, don't forget to change the API key in values/google_maps_api.xml
https://github.com/xomena-so/so41431384
我希望这会有所帮助!
这篇关于在Android版Google地图中突出显示整个国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!