Android Open Source - MockLocation Address Lookup Task






From Project

Back to project page MockLocation.

License

The source code is released under:

MIT License

If you think the Android project MockLocation listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.headdetect.android.mocklocation.Tasks;
/*from  w w w  .ja  v  a2s .  c  om*/
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.util.Log;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;

public class AddressLookupTask extends AsyncTask<LatLng, Void, String> {

    Context mContext;
    GoogleMap mMap;
    MarkerOptions mMarkerOptions;


    public AddressLookupTask(Context context, GoogleMap map, MarkerOptions markerOptions) {

        super();
        this.mContext = context;
        this.mMap = map;
        this.mMarkerOptions = markerOptions;

    }

    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder geocoder = new Geocoder(mContext);
        double lat = params[0].latitude;
        double lng = params[0].longitude;

        List<Address> addresses = null;
        StringBuilder actualAddress = new StringBuilder();

        try {
            addresses = geocoder.getFromLocation(lat, lng, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (addresses != null) {
            for (Address address : addresses) {
                actualAddress.append(String.format("%s, %s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getLocality(), address.getCountryName()) + "\n");
            }
        }

        return actualAddress.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        mMarkerOptions.title(result);
        mMarkerOptions.visible(true);

        Log.d("MockLocation", result);

        mMap.addMarker(mMarkerOptions).showInfoWindow();
    }

}




Java Source Code List

com.headdetect.android.mocklocation.ApplicationTest.java
com.headdetect.android.mocklocation.LocationMapFragment.java
com.headdetect.android.mocklocation.MainActivity.java
com.headdetect.android.mocklocation.NavigationDrawerFragment.java
com.headdetect.android.mocklocation.Tasks.AddressLookupTask.java