【发布时间】: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