我是新来的,因此,请问一下,如果已经问了这个问题(通常不是),或者作为一个新手,也许我做错了。
所以有问题。
我想在Google map android(我的geojson)中居中。
有我的geoJson。
{
"type": "Polygon",
"coordinates": [
[
[
2.1096056699752808,
49.007069721997176
],
[
2.1101635694503784,
49.00640113120101
],
[
2.1103888750076294,
49.00579235387832
],
[
2.1109735965728755,
49.00497770668481
],
[
2.1093428134918213,
49.004180641564616
],
[
2.10762619972229,
49.003237519337205
],
[
2.1063923835754395,
49.00271668298914
],
[
2.104933261871338,
49.0023225329428
],
[
2.103109359741211,
49.00157645467159
],
[
2.1024978160858154,
49.00125268137821
],
[
2.102004289627075,
49.000830365223656
],
[
2.1010172367095947,
48.99976048160506
],
[
2.1006417274475098,
48.9995141235692
],
[
2.0986783504486084,
48.99873280859517
],
[
2.0958673954010005,
48.997838856609434
],
[
2.093796730041504,
48.99738131592137
],
[
2.0936572551727295,
48.99733204205815
],
[
2.09065318107605,
48.998359743969125
],
[
2.0894408226013184,
48.99889470369554
],
[
2.088373303413391,
48.99951060415987
],
[
2.0878690481185913,
49.00182632175247
],
[
2.0877134799957275,
49.002618145770015
],
[
2.087423801422119,
49.003913190807005
],
[
2.0922625064849854,
49.0047999956991
],
[
2.09663987159729,
49.00562344324437
],
[
2.101478576660156,
49.006489104187075
],
[
2.104707956314087,
49.007101391864836
],
[
2.1054375171661377,
49.0071295428414
],
[
2.1062850952148438,
49.00706620312175
],
[
2.108291387557983,
49.007059165370144
],
[
2.1096056699752808,
49.007069721997176
]
]
],
"properties": {
"Territoire" : 2
}
}
我可以将其放入字符串,然后放入JsonObject。像这样 :
String json = loadJSONFromAsset(ville, terr);
try {
JSONObject obj = new JSONObject(json);
GeoJsonLayer layer = new GeoJsonLayer(mMap, obj);
layer.addLayerToMap();
(......)
} catch (JSONException e) {
e.printStackTrace();
}
public String loadJSONFromAsset(String ville, String terr) {
String json = null;
try {
InputStream is = getAssets().open(ville+"/"+terr+".geojson");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
我想我在这里很好.... :-D
但是知道我想以良好的缩放级别将图层集中在我的谷歌地图中...存在问题!
我已经花时间在互联网上,但做不到。我尝试了一些但不起作用:
JSONArray jsonArray = obj.getJSONArray("coordinates");
ArrayList<LatLng> listdata = new ArrayList<LatLng>();
if (jsonArray != null) {
for (int i=0;i<jsonArray.length();i++){
String[] coord = jsonArray.toString().split(",");
double x = Double.parseDouble(coord[0]);
double y = Double.parseDouble(coord[1]);
listdata.add(new LatLng(x,y));
}
}
LatLng center = getPolygonCenterPoint(listdata);
mMap.moveCamera(CameraUpdateFactory.newLatLng(center));
private LatLng getPolygonCenterPoint(ArrayList<LatLng> polygonPointsList){
LatLng centerLatLng = null;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int i = 0 ; i < polygonPointsList.size() ; i++)
{
builder.include(polygonPointsList.get(i));
}
LatLngBounds bounds = builder.build();
centerLatLng = bounds.getCenter();
return centerLatLng;
}
有人知道答案吗?
谢谢 !
约旦
最佳答案
您可以将地图居中到正在生成的LatLngBounds bounds
对象的完整扩展名(documentation):
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));
请注意,您可以设置填充(我将其设置为0)
在您的代码中:
private LatLngBounds getPolygonBounds(ArrayList<LatLng> polygonPointsList){
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int i = 0 ; i < polygonPointsList.size() ; i++) {
builder.include(polygonPointsList.get(i));
}
return builder.build();
}
使用以下命令将其居中:
LatLngBounds bounds = getPolygonBounds(listdata);
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));