简体   繁体   English

在Monodroid上实施的GoogleMaps Android应用

[英]GoogleMaps Android app implementing on Monodroid

This code is showing the address on a google maps sending the address text to an url and then retrieving it in a Json object, Is there a way to transalate this code to Monodroid using Httppost from apache libraries or not? 此代码在Google地图上显示地址,将地址文本发送到url,然后在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;
}

} }

HttpPost isn't bound, and it's currently very hard to bind your own Java libraries. HttpPost没有绑定,现在绑定您自己的Java库非常困难。

The easiest way to do this would be to use the equivalent .NET classes, like HttpWebRequest or WebClient. 最简单的方法是使用等效的.NET类,例如HttpWebRequest或WebClient。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM