0

I am building an android application where I user selected the area from Auto-completed text. Now the problem is I need the latLng of that selected address. I google a lot and finally I found the Google API that provide the respective latLng from the address. But When I am using that Google API but in my case there is a problem that is - Attempt to invoke virtual method 'int java.lang.String.length()' My code is below -

      // 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();

        }

    }
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
sofquestion 9
  • 167
  • 4
  • 12
  • Depending on your needs, you might be able to go with a more simple approach and just use the Geocoder class, example here: http://stackoverflow.com/questions/29463160/android-getfromlocationname-returns-size-0-in-address-list/29463865#29463865 – Daniel Nugent Aug 05 '15 at 20:29
  • looks like a dupe of [this](http://stackoverflow.com/questions/3574644/how-can-i-find-the-latitude-and-longitude-from-address) – andrewdleach Aug 05 '15 at 20:35
  • Take look at [this](http://javapapers.com/android/android-geocoding-to-get-latitude-longitude-for-an-address/), I tired it, and it worked. – bjiang Aug 06 '15 at 17:01

0 Answers0