Guest User

Untitled

a guest
Dec 11th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. //Button btnShowCoord;
  2. //EditText edtAddress;
  3. TextView txtCoord;
  4. private GoogleMap mMap;
  5.  
  6. String lat1 = "0", lng1 = "0";
  7. String adr = "проспект Маршала Жукова 24";
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13.  
  14. new GetCoordinates().execute(adr.replace(" ","+"));
  15.  
  16. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  17. .findFragmentById(R.id.map);
  18. mapFragment.getMapAsync(this);
  19.  
  20. //btnShowCoord = (Button) findViewById(R.id.btnShowCoordinates);
  21. //edtAddress = (EditText) findViewById(R.id.edtAddress);
  22. txtCoord = (TextView) findViewById(R.id.txtCoordinates);
  23.  
  24. }
  25.  
  26. private class GetCoordinates extends AsyncTask<String,Void,String> {
  27. ProgressDialog dialog = new ProgressDialog(MainActivity.this);
  28.  
  29. @Override
  30. protected void onPreExecute() {
  31. super.onPreExecute();
  32. dialog.setMessage("Please wait....");
  33. dialog.setCanceledOnTouchOutside(false);
  34. dialog.show();
  35. }
  36.  
  37. @Override
  38. protected String doInBackground(String... strings) {
  39. String response;
  40. try{
  41. String address = strings[0];
  42. HttpDataHandler http = new HttpDataHandler();
  43. String url = String.format("https://maps.googleapis.com/maps/api/geocode/json?address=%s",address);
  44. response = http.getHTTPData(url);
  45. return response;
  46. }
  47. catch (Exception ex)
  48. {
  49.  
  50. }
  51. return null;
  52. }
  53.  
  54. @Override
  55. protected void onPostExecute(String s) {
  56. try{
  57. JSONObject jsonObject = new JSONObject(s);
  58.  
  59. String lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry")
  60. .getJSONObject("location").get("lat").toString();
  61. String lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry")
  62. .getJSONObject("location").get("lng").toString();
  63.  
  64. lat1 = lat;
  65. lng1 = lng;
  66.  
  67. txtCoord.setText(String.format("Coordinates : %s / %s ",lat,lng));
  68.  
  69. if(dialog.isShowing())
  70. dialog.dismiss();
  71.  
  72. } catch (JSONException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77.  
  78. @Override
  79. public void onMapReady(GoogleMap googleMap) {
  80. mMap = googleMap;
  81.  
  82. // Add a marker in Sydney and move the camera
  83. LatLng sydney = new LatLng(Double.parseDouble(lat1), Double.parseDouble(lng1));
  84. mMap.addMarker(new MarkerOptions().position(sydney).title("Marker"));
  85. mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  86. }}`
Add Comment
Please, Sign In to add comment