0

I have the list of the Address(not Lat,Lng) which is on the other activity. Now I want to pass those address into my MapActivity via Intent extras and geocode it. How can I do this?

Below is my address data which is in my Listview activity

viewHolder.address.setText(row[4]);//Read from CSV file
1
  • You will have to reverse geocode the address into lat/lng. And then plot them on the map as markers. Have a look at the Geocoder class. Commented Apr 25, 2016 at 8:45

2 Answers 2

1

Pass your data to activity using list or map and use following code to get the lat long from address

public void getLocationFromAddress(String strAddress){

   Geocoder coder = new Geocoder(this);
   List<Address> address;
   GeoPoint p1 = null;

   try {
        address = coder.getFromLocationName(strAddress,5);
        if (address==null) {
        return null;
    }
    Address location=address.get(0);
    location.getLatitude();
    location.getLongitude();

    Log.i("TAG","Lat = "+location.getLatitude())
    Log.i("TAG","Long = "+location.getLongitude())
    }
}

Also Check This Geocoding Example

Sign up to request clarification or add additional context in comments.

1 Comment

Ty for that . But how can I pass the address ?
1
  public class GeoData {
        private String address,lat,lng;
        public GeoData() {
             this("", "0", "0");
        }

    public GeoData(String address, String lat, String lng) {
        this.address = address;
        this.lat = lat;
        this.lng = lng;
    }
    public String getAddress() {
        return address;
    }

    public String getLat() {
        return lat;
    }

    public String getLng() {
        return lng;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }
}

Create below methods in your activity.

private GeoData getAddresesFronJson(JSONObject jsonObj) {
    if (jsonObj == null) {
        return null;
    }
    try {
        // Create a JSON object hierarchy from the results
        JSONArray resultJsonArray = jsonObj.getJSONArray("results");

        // Extract the Place descriptions from the results
        ArrayList<GeoData> resultList = new ArrayList<GeoData>(
                resultJsonArray.length());
        JSONObject locationObject = null;
        for (int i = 0; i < resultJsonArray.length(); i++) {
            GeoData data = new GeoData();
            data.setAddress(resultJsonArray.getJSONObject(i).getString(
                    "formatted_address"));
            locationObject = resultJsonArray.getJSONObject(i)
                    .getJSONObject("geometry").getJSONObject("location");

            data.setLat(locationObject.getString("lat"));

            data.setLng(locationObject.getString("lng"));

            return data;
        }

    } catch (JSONException e) {
        Log.e("JSONObject", "Cannot process JSON results", e);
    }
    return null;
}




private void computeAddress(String address) {
    address = address.replaceAll(" ", "%20");
    String apiKey = "YOUR PROJECT API KEY(Genarate from google console))";
    String url = "https://maps.googleapis.com/maps/api/geocode/json?address="
            + address + "&key=" + apiKey;
    // Log.i("googleapis--url:", url.toString());

    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET, url, null,
            new com.android.volley.Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject jsonObject) {
                    GeoData geoData = getAddresesFronJson(jsonObject);
                    if (geoData == null) {
                        return;
                    }
                    // Use this geoData object
                }

            }, new com.android.volley.Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
}

Just use computeAddress(String address) method for getting the Geocode object. By using Geocode you can display the address markers in the map.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.