我想在谷歌地图api上传递我的项目地址并在谷歌地图中显示地址

时间:2015-09-22 05:49:38

标签: android google-maps

我有editText用于编写地址并获得此活动中下一个活动的意图我希望传递地址,并且在获得经度和经度之后我想在Google地图上看到。

这是我的代码以及asyn Class

_DEBUG

1 个答案:

答案 0 :(得分:0)

您必须使用Handler或AsyncTask来调用从服务器获取响应:

将getLatLongFromAddress()放在同一个类中,

var supervisorStudents = researchers.Where(r => r.supervisor_id == 0).
            ToDictionary(supervisor => supervisor.id, supervisor => 
                researchers.Where(student => student.supervisor_id == supervisor.id).Select(student => student.id).Count());

的AsyncTask:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.geotracking);

    SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    // Getting a reference to the map
    googleMap = supportMapFragment.getMap();
    Intent i = getIntent();
    String location = i.getStringExtra("address");

    getLatLongFromAddress(location);
}

public void getLatLongFromAddress(String youraddress) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            String uri = "http://maps.google.com/maps/api/geocode/json?address="
                    + youraddress + "&sensor=false";
            HttpGet httpGet = new HttpGet(uri);
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            StringBuilder stringBuilder = new StringBuilder();

            try {
                response = client.execute(httpGet);
                HttpEntity entity = response.getEntity();
                InputStream stream = entity.getContent();
                int b;
                while ((b = stream.read()) != -1) {
                    stringBuilder.append((char) b);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject = new JSONObject(stringBuilder.toString());

                double lng = ((JSONArray) jsonObject.get("results"))
                        .getJSONObject(0).getJSONObject("geometry")
                        .getJSONObject("location").getDouble("lng");

                double lat = ((JSONArray) jsonObject.get("results"))
                        .getJSONObject(0).getJSONObject("geometry")
                        .getJSONObject("location").getDouble("lat");

                Log.d("latitude", "" + lat);
                Log.d("longitude", "" + lng);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }).start();
}