1

Please, can anyone help me?, I'm really desperate

I'm developing a android application to view a certain address from an autocomplete list on a mapview using the Google Places API,and when the user clicks on the item, it should send to where the address is on the mapview, So here is the code

public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements     Filterable {
private static final String LOG_TAG = "SEACRHPLACES";
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_KEY = "AIzaSyDTOyqTAWYJfTB3aJ0yh2V1Jmx2OvKG3pA";
private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?sensor=false&key=" + API_KEY);
sb.append("&components=country:eg");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());   
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
return resultList;
}
private ArrayList<String> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());     
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
}

and here's is the mapview activity

public class MapViewActivity extends MapActivity {
MapView mapView;
List<Overlay> mapOverlays;
AddItemizedOverlay itemizedOverlay;

GeoPoint geoPoint;
MapController mc;
double latitude;
double longitude;
OverlayItem overlayitem;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_view);
    //getLocation from user address input
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapOverlays = mapView.getOverlays();
    Drawable drawable_user = this.getResources()
            .getDrawable(R.drawable.mark_red);
    itemizedOverlay = new AddItemizedOverlay(drawable_user, this);
    overlayitem = new OverlayItem(geoPoint, "Your Location",
            "That is you!");
    itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);
    itemizedOverlay.populateNow();
    Drawable drawable = this.getResources()
            .getDrawable(R.drawable.mark_blue);
    itemizedOverlay = new AddItemizedOverlay(drawable, this);
    mc = mapView.getController();
    int minLat = Integer.MAX_VALUE;
    int minLong = Integer.MAX_VALUE;
    int maxLat = Integer.MIN_VALUE;
    int maxLong = Integer.MIN_VALUE;        
    mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
    mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
    mapView.postInvalidate();
}
@Override
protected boolean isRouteDisplayed() {
    return false;
}
     }

so how do i retrieve the location of the address input to view on mapview?

4

0 回答 0