Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Button btnShowCoord;
- //EditText edtAddress;
- TextView txtCoord;
- private GoogleMap mMap;
- String lat1 = "0", lng1 = "0";
- String adr = "проспект Маршала Жукова 24";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- new GetCoordinates().execute(adr.replace(" ","+"));
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- //btnShowCoord = (Button) findViewById(R.id.btnShowCoordinates);
- //edtAddress = (EditText) findViewById(R.id.edtAddress);
- txtCoord = (TextView) findViewById(R.id.txtCoordinates);
- }
- private class GetCoordinates extends AsyncTask<String,Void,String> {
- ProgressDialog dialog = new ProgressDialog(MainActivity.this);
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- dialog.setMessage("Please wait....");
- dialog.setCanceledOnTouchOutside(false);
- dialog.show();
- }
- @Override
- protected String doInBackground(String... strings) {
- String response;
- try{
- String address = strings[0];
- HttpDataHandler http = new HttpDataHandler();
- String url = String.format("https://maps.googleapis.com/maps/api/geocode/json?address=%s",address);
- response = http.getHTTPData(url);
- return response;
- }
- catch (Exception ex)
- {
- }
- return null;
- }
- @Override
- protected void onPostExecute(String s) {
- try{
- JSONObject jsonObject = new JSONObject(s);
- String lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry")
- .getJSONObject("location").get("lat").toString();
- String lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry")
- .getJSONObject("location").get("lng").toString();
- lat1 = lat;
- lng1 = lng;
- txtCoord.setText(String.format("Coordinates : %s / %s ",lat,lng));
- if(dialog.isShowing())
- dialog.dismiss();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- // Add a marker in Sydney and move the camera
- LatLng sydney = new LatLng(Double.parseDouble(lat1), Double.parseDouble(lng1));
- mMap.addMarker(new MarkerOptions().position(sydney).title("Marker"));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
- }}`
Add Comment
Please, Sign In to add comment