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