【问题标题】:Extract coordinates lat lng from geojson feature collection in java/Android Studio从 java/Android Studio 中的 geojson 特征集合中提取坐标 lat lng
【发布时间】:2017-05-23 00:32:38
【问题描述】:

我是 java 新手,也是 android 新手,我正在尝试创建一个 google maps 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 地图中有一个“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))));

非常感谢。

【问题讨论】:

  • "在Android studio/java/google maps有一个"有一个什么?可以请你结束你的句子吗?

标签: java google-maps coordinates extract geojson


【解决方案1】:
  1. 您需要在 gradle 的依赖项之间添加以下代码

依赖{ 编译 'com.google.maps.android:android-maps-utils:0.5+' }

  1. 在onCreate中添加这段代码

GeoJsonLayer layer = new GeoJsonLayer(mMap, jObjek); //mMap 是 GoogleMap 对象和 jObjek 是您的 geojson,如果您从 API 获取 geojson,请尝试使用 volley

  1. 将 GeoJsonLayer 添加到地图

layer.addLayerToMap();

希望对您有所帮助。

【讨论】:

  • 非常感谢您的帮助。同时,我通过解决方法避免了这个问题。没有更多时间回去尝试这个,但再次感谢您的帮助。我希望它对其他人有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 2016-02-01
  • 2021-07-11
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
相关资源
最近更新 更多