package com.testcase;
import java.io.IOException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.gson.Gson;
public class MapDirectionsDemo extends MapActivity {
private ProgressDialog progressDialog;
private final String TAG = MapDirectionsDemo.class.getName();
private final String startLocation = "30.500070,114.344270";
private final String endLocation = "30.505140,114.383220";
//请求direction 的地址
private final String URL = "http://maps.google.com/maps/api/directions/json?origin=" + startLocation
+ "&destination=" + endLocation
+ "&mode=driving"
+ "&sensor=false"
+ "&language=zh-CN";
private MapView mapView;
// use a handler to update the UI (send the handler messages from other threads)
private final Handler handler = new Handler() {
@Override
public void handleMessage(final Message msg) {
progressDialog.dismiss();
String bundleResult = msg.getData().getString("RESPONSE");
intoMapView(parseDirection(bundleResult));
}
};
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.map_direction_demo);
mapView = (MapView)this.findViewById(R.id.mapDirectionDemo);
mapView.setBuiltInZoomControls(true);
performRequest();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (this.progressDialog != null && this.progressDialog.isShowing()) {
this.progressDialog.dismiss();
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/**
* http 请求
*/
private void performRequest() {
// use a response handler so we aren't blocking on the HTTP request
final ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
public String handleResponse(HttpResponse response) {
// when the response happens close the notification and update UI
StatusLine status = response.getStatusLine();
Log.d(TAG, " statusCode - " + status.getStatusCode());
Log.d(TAG, " statusReasonPhrase - "
+ status.getReasonPhrase());
HttpEntity entity = response.getEntity();
String result = null;
try {
if (status.getStatusCode() == 200) {
result = EntityUtils.toString(entity);
Message message = handler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString("RESPONSE", result);
message.setData(bundle);
handler.sendMessage(message);
}
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return result;
}
};
this.progressDialog = ProgressDialog.show(this, "working . . .", "performing HTTP request");
// do the HTTP dance in a separate thread (the responseHandler will fire when complete)
new Thread() {
@Override
public void run() {
try {
HttpGet request = new HttpGet(URL);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 6000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
httpClient.execute(request, responseHandler);
} catch (ClientProtocolException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
}.start();
}
/**
* 解析Direction JSON
* @param jsonData
* @return
*/
private Direction parseDirection(String jsonData) {
Gson gson = new Gson();
Direction direction = gson.fromJson(jsonData, Direction.class);
return direction;
}
/**
* 进入地图
* @param direction
*/
private void intoMapView(Direction direction) {
List<GeoPoint> list = Kit.decodePoly(direction.getRoutes().get(0).getOverview_polyline().getPoints());
mapView.getController().setZoom(12);
mapView.getController().animateTo(list.get(0));
mapView.getOverlays().add(new PolyLineLayer(list));
}
}