如何从android中的地址名称获取lat lng?

时间:2015-08-05 20:18:17

标签: java android google-maps google-maps-android-api-2 latitude-longitude

我正在构建一个Android应用程序,用户从自动完成的文本中选择了该区域。现在问题是我需要所选地址的latLng。我谷歌了很多,最后我找到了从地址提供相应latLng的Google API。但是当我使用那个谷歌API但在我的情况下存在一个问题 - 尝试调用虚拟方法' int java.lang.String.length()' 我的代码在下面 -

      // http://maps.googleapis.com/maps/api/geocode/json?address=NewYork&sensor=true
    private class GetAddress extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            ServiceHandler http = new ServiceHandler();

            try {
                stringPickerUserAddress = URLEncoder.encode(stringPickerUserAddress, "utf-8");
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            final String url = "http://maps.googleapis.com/maps/api/geocode/json?address="
                    + stringPickerUserAddress + "&sensor=true";
            String jsonString = http.makeServiceCall(url, ServiceHandler.POST,
                    null);
            if (jsonString != null) {
                try {
                    address = new JSONObject(jsonString);
                    results = address.getJSONArray(Tag_results);
                    for (int i = 0; i < results.length(); i++) {
                        JSONObject temp_obj = results.getJSONObject(i);
                        geometry = temp_obj.getJSONObject(Tag_geometry);
                        location = geometry.getJSONObject(Tag_location);
                        result_lat = location.getString(Tag_lat);
                        result_lng = location.getString(Tag_lng);

                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub

            // latitude and longitude
            latitude = Double.parseDouble(result_lat);
            longitude = Double.parseDouble(result_lng);


//            sourceLatLng = new LatLng(latitude, longitude); // //    markerPoints.add(sourceLatLng); // //            // Creating MarkerOptions //            MarkerOptions options = new MarkerOptions(); // //            // Setting the position of the marker //            options.position(sourceLatLng); //            options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); //            map.addMarker(options); // //            Criteria criteria = new Criteria(); //            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //            String provider = locationManager.getBestProvider(criteria, false); //        Location location = locationManager.getLastKnownLocation(provider); // //            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng( //                    latitude, longitude)); //            CameraUpdate zoom = CameraUpdateFactory.zoomTo(18); //            map.moveCamera(center); //            map.animateCamera(zoom); // //   new GetAddressdestination().execute();

        }

    }

0 个答案:

没有答案