从Mapview上的自动完成文本视图查看选定的地址

时间:2012-11-23 10:09:54

标签: java android json google-places-api

拜托,任何人都可以帮助我吗?我真的很绝望

我正在开发一个Android应用程序,使用Google Places API从mapview上的自动填充列表中查看某个地址,当用户点击该项目时,它应该发送到地图在地图视图上的位置,所以这是代码

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

这里是mapview活动

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

那么如何检索地址输入的位置以在mapview上查看?

0 个答案:

没有答案