【问题标题】:How to get LatLng data from Google Geocoding API json?如何从 Google Geocoding API json 获取 LatLng 数据?
【发布时间】:2016-04-22 00:50:30
【问题描述】:

我正在使用地图创建应用程序并遇到问题。 我的 Fragment 和 AutoCompleteTextView 中有一个 mapView。当我从自动完成中选择任何城市时,它应该在地图上显示为一个标记。

这是代码,它在真正的三星 Galaxy S3 和 Galaxy S6 模拟器上运行良好,但在魅族 MX5 上运行良好。 正如我发现的,Geocoder 不能在每台设备上正常工作,所以有一个使用 Google Geocoding API 的解决方案。我得到了 Json 答案,但不知道如何从 JSON 中获取 LatLang 和 FeatureName。

JSON 示例:http://maps.google.com/maps/api/geocode/json?address=London&sensor=true

我是 Android 新手,如果不是很清楚,请见谅。

atvFrom.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            locationFrom = atvFrom.getText().toString();
            List<Address> addressListFrom = null;
            Geocoder geocoder = new Geocoder(getActivity());

            try {
                if (markerFrom[0] != null) {markerFrom[0].remove();}
                addressListFrom = geocoder.getFromLocationName(locationFrom, 1);

                if (addressListFrom != null) {
                    Address addressFrom = addressListFrom.get(0);
                    latLngFrom[0] = new LatLng(addressFrom.getLatitude(), addressFrom.getLongitude());
                    tripFrom = addressListFrom.get(0).getFeatureName();

                    markerFrom[0] = googleMap.addMarker(new MarkerOptions().position(latLngFrom[0]).title("From"));
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLngFrom[0]));

                    if (markerTo[0]!=null) {
                        if (line[0] !=null) {line[0].remove();}
                        line[0] = googleMap.addPolyline(new PolylineOptions().add(latLngFrom[0], latLngTo[0]).width(5).color(Color.BLUE));
                    }

                } else {
                    MyGeocoder myGeocoder = new MyGeocoder(FragmentMap.this);
                    myGeocoder.execute(locationFrom);
                    try {
                        JSONObject jObj = new JSONObject(myGeocoder.jsonResult);
                        String Status = jObj.getString("status");
                        if (Status.equalsIgnoreCase("OK")) {
                            JSONArray Results = jObj.getJSONArray("results");
                            // how to get LatLng and FeatureName from json?
                            // dont know what to do here

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            } catch (IOException e) {
                //e.printStackTrace();
                Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
            }
        }
    });

【问题讨论】:

    标签: java android google-maps


    【解决方案1】:

    您需要浏览 Json 对象才能到达 location。这就是你需要做的事情

    try {
        JSONObject jObj = new JSONObject(myGeocoder.jsonResult);
        String Status = jObj.getString("status");
        if (Status.equalsIgnoreCase("OK")) {
            JSONArray results = jObj.getJSONArray("results");
            JSONObject item = results.getJSONObject(0);//read first item of results
            JSONObject geometry = item.getJSONObject("geometry");//location is inside geometry object
            JSONObject location = geometry.getJSONObject("location");
            double latitude = location.getDouble("lat");
            double longitude = location.getDouble("lng");
            //or
            LatLng latLng = new LatLng(latitude, longitude);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    

    要检查设备上的 Geocoder 是否存在,请调用以下方法

    使用 isPresent() 方法确定是否存在 Geocoder 实现。

    if(Geocoder.isPresent()){
       //exists
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多