簡體   English   中英

在Monodroid上實施的GoogleMaps Android應用

[英]GoogleMaps Android app implementing on Monodroid

此代碼在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沒有綁定,現在綁定您自己的Java庫非常困難。

最簡單的方法是使用等效的.NET類,例如HttpWebRequest或WebClient。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM