Monodroid上的Google Maps Android应用程序实施

时间:2012-01-31 22:40:59

标签: c# java android xamarin.android

此代码显示谷歌地图上的地址,将地址文本发送到网址,然后在Json对象中检索它,是否有办法使用来自apache库的Httppost将此代码转换为Monodroid?

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

}

1 个答案:

答案 0 :(得分:0)

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

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