gpt4 book ai didi

java - 谷歌地图 Android 中的中心 GeoJson

转载 作者:行者123 更新时间:2023-11-30 01:15:47 26 4
gpt4 key购买 nike

我是新来的,所以请同情,如果这个问题已经被问过(通常不会)或者作为新手,也许我做错了。

那么问题来了。

我想以 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;
}

有人知道答案吗?

谢谢!

乔丹

最佳答案

您可以将 map 居中到您正在生成的 LatLngBounds 边界 对象的完整扩展 (documentation):

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));

注意可以设置一个padding(我这里设置为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));

关于java - 谷歌地图 Android 中的中心 GeoJson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com