谷歌地图Android应用程序在Monodroid上实现

本文关键字:实现 Monodroid Android 应用程序 谷歌地图 | 更新日期: 2023-09-27 18:22:15

这段代码在谷歌地图上显示地址,将地址文本发送到url,然后在Json对象中检索。有没有办法使用apache库中的Httppost将这段代码交易到Mondroid?

public class MapsActivity extends MapActivity {
Geocoder geocoder = null;
MapView mapView = null;
ProgressDialog progDialog = null;
List<Address> addressList = null;
double latitude;
double longitude;
@Override
protected boolean isRouteDisplayed() {
    return false;
}
@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    mapView = (MapView) findViewById(R.id.geoMap);
    progDialog = ProgressDialog.show(MapsActivity.this, "Processing...",
            "Finding Location...", true, false);
    TextView txtAddress = (TextView)findViewById(R.id.location);
    String locationName = "Av. Venezuela 1234 Lima Peru";
    txtAddress.setText(locationName);
    JSONObject location = getLocationInfo(locationName);
    Boolean bool = getLatLong(location);
    if(location!=null && bool != false){
        int lat = (int) (latitude * 1000000);
        int lng = (int) (longitude * 1000000);
        GeoPoint pt = new GeoPoint(lat, lng);
        mapView.getController().setZoom(16);
        mapView.getController().setCenter(pt);
        mapView.getController().animateTo(pt);
    }else {
        Dialog foundNothingDlg = new AlertDialog.Builder(
                MapsActivity.this).setIcon(0)
                .setTitle("Failed to Find Location")
                .setPositiveButton("Ok", null)
                .setMessage("Location Not Found...").create();
        foundNothingDlg.show();
    }
}


public JSONObject getLocationInfo(String address) {
    StringBuilder stringBuilder = new StringBuilder();
    try {
    address = address.replaceAll(" ","%20");    
    HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
        response = client.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
        Log.i("JSONOBJECT: ", stringBuilder.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonObject;
}
public boolean getLatLong(JSONObject jsonObject) {
    try {
        longitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");
        latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");
    } catch (JSONException e) {
        return false;
    }
    progDialog.dismiss();
    return true;
}

}

谷歌地图Android应用程序在Monodroid上实现

HttpPost没有绑定,目前很难绑定自己的Java库。

最简单的方法是使用等效的.NET类,如HttpWebRequest或WebClient。