Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by Naresh ( 12 years ago )
public class MainActivity extends Activity implements LocationListener {

 // providers
 public LocationManager locationManager;
 String strNetworkProvider;
 public TextView tvAddress;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  // Get the Location Manager
   String locati
  locati getSystemService(location_context);
  strNetworkProvider = LocationManager.NETWORK_PROVIDER;
  locationManager.requestLocationUpdates(strNetworkProvider, 0, 0,
    MainActivity.this);
  tvAddress = (TextView) findViewById(R.id.address);
 }

 // Get the current Location
 @Override
 public void onLocationChanged(Location location) {
  if (location != null) {

   locationManager.removeUpdates(MainActivity.this);
   double latitude = location.getLatitude();
    double l
   Double[] geopoints = { latitude, longitude };
   Log.i("Latitude and Logitude", latitude + ", " + longitude);

   // async task for initial WS call
   new getAddressFromGeopoints().execute(geopoints);

  }
 }

 @Override
 public void onProviderDisabled(String arg0) {
 }

 @Override
 public void onProviderEnabled(String provider) {
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
 }

 // Async task for getting the address
 public class getAddressFromGeopoints extends
   AsyncTask<Double, Void, List<Address>> {

  @Override
  protected List<Address> doInBackground(Double... geoPoints) {
   List<Address> addresses;
   addresses = getFromLocation(geoPoints[0], geoPoints[1], 1);
   return addresses;
  }

  protected void onPostExecute(final List<Address> addresses) {
   if (addresses != null && addresses.size() > 0) {
    String address = addresses.get(0).getAddressLine(0);
    tvAddress.setText(address);
    Log.i("Address", address);
   }
  }
 }

 // This method is responsible for get the address from the
 // Geopoints(Latitude and Longitude)
 public static List<Address> getFromLocation(double lat, double lng,
   int maxResult) {

  String address = String
    .format(Locale.ENGLISH,
      "http://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=true&language;="
        + Locale.getDefault().getCountry(), lat, lng);
  HttpGet httpGet = new HttpGet(address);
  HttpClient client = new DefaultHttpClient();
  HttpResponse response;
  StringBuilder stringBuilder = new StringBuilder();

  List<Address> retList = null;

  try {
    resp
   HttpEntity entity = response.getEntity();
   InputStream stream = entity.getContent();
   int b;
   while ((b = stream.read()) != -1) {
    stringBuilder.append((char) b);
   }

    JSONObject js JSONObject();
    js JSONObject(stringBuilder.toString());

   retList = new ArrayList<Address>();

   if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
    JSONArray results = jsonObject.getJSONArray("results");
    for (int i = 0; i < results.length(); i++) {
     JSONObject result = results.getJSONObject(i);
     String indiStr = result.getString("formatted_address");
     Address addr = new Address(Locale.ITALY);
     addr.setAddressLine(0, indiStr);
     retList.add(addr);
    }
   }

  } catch (ClientProtocolException e) {
   Log.e(MainActivity.class.getName(),
     "Error calling Google geocode webservice.", e);
  } catch (IOException e) {
   Log.e(MainActivity.class.getName(),
     "Error calling Google geocode webservice.", e);
  } catch (JSONException e) {
   Log.e(MainActivity.class.getName(),
     "Error parsing Google geocode webservice response.", e);
  }

  return retList;
 }
}

 

Revise this Paste

Your Name: Code Language: