gpt4 book ai didi

java - 从 java/Android Studio 中的 geojson 特征集合中提取坐标 lat lng

转载 作者:行者123 更新时间:2023-12-01 09:02:52 25 4
gpt4 key购买 nike

我是java新手,android新手,我正在尝试创建一个谷歌地图android应用程序,它将使用城市的公交系统API来搜索公交车站,实时发车,..

API 返回一个 GeoJSON 文件,如下所示:

{

"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [6.113204, 49.61028]
},
"properties": {
"id": 200403005,
"name": "Belair, Sacré-Coeur"
}
},
...
]

}

事实上,该文件非常大 - 城市中的所有公交车站。

我需要找到距离用户 GPS 位置最近的公交车站。

为了做到这一点,我打算提取所有坐标: “坐标”:[6.113204,49.61028]解析它们并找出哪一个更接近用户位置。

Google 文档和我能找到的有关此问题的其他文档很少,而且不适合初学者......

在 Android studio/java/google map 中,有一个“feature.getGeometry()”的东西,但我无法更深入地到达坐标级别并获取它们。

这就是我正在尝试做的事情:

 // Iterate over all the features stored in the layer
for (GeoJsonFeature feature : layer.getFeatures()) {
// Check if the feature is what i need
if (feature.getProperty("id") != null && feature.hasProperty("name")) {
//Here I need to get the coordinates and create an array which i can use further to find the closest bus stop to user position like below (or similar).

显然没有什么可以超越“几何”级别的坐标?

我的意思是,我看到我可以执行“feature.getProperty(“id”)”并使用它,但显然我不能执行“feature.getGeometry(“coordinates”)”或类似的操作?

查找最近公交车站的代码:

for (int i = 0; i < jsonArray_buses.length(); i++) {
JSONObject jsonObject = jsonArray_buses.getJSONObject(i);
double lng = Double.parseDouble(jsonArray_buses.getJSONObject(i).getString("longitude"));
double lat = Double.parseDouble(jsonArray_buses.getJSONObject(i).getString("latitude"));

Log.e("TAG_latlong", "latitude:" + latitude + " longitude:" + longitude + "marker_lat:" + lat + "marker_long:" + lng);

Location locationA = new Location("point A");
locationA.setLatitude(lat);
locationA.setLongitude(lng);

Location locationB = new Location("point B");
locationB.setLatitude(latitude);
locationB.setLongitude(longitude);


float min_distance_old = min_distance;
min_distance = min(min_distance, locationA.distanceTo(locationB));

if (min_distance_old != min_distance) {
closest = i;
}

} //end for


JSONObject display_jsonObject = jsonArray_buses.getJSONObject(closest);
//save
double lng = Double.parseDouble(jsonArray_buses.getJSONObject(closest).getString("longitude"));
double lat = Double.parseDouble(jsonArray_buses.getJSONObject(closest).getString("latitude"));
markerList.add(mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA))
.title(display_jsonObject.getString("name"))
.snippet(Integer.toString((display_jsonObject.getInt("id"))))
.position(new LatLng(lat, lng))));

非常感谢。

最佳答案

  1. 您需要在依赖项之间的 gradle 中添加以下代码

dependencies { compile 'com.google.maps.android:android-maps-utils:0.5+' }

  • 在 onCreate 中添加此代码
  • GeoJsonLayer layer = new GeoJsonLayer(mMap, jObjek); //mMap is GoogleMap object and jObjek is your geojson, if you take geojson from API try to use volley

  • 将 GeoJsonLayer 添加到 map
  • layer.addLayerToMap();

    希望这有帮助。

    关于java - 从 java/Android Studio 中的 geojson 特征集合中提取坐标 lat lng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41536304/

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