Advertisement
eranseg

City Map and Info

Nov 5th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.17 KB | None | 0 0
  1. package com.sgl.cityviewer;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.fragment.app.FragmentActivity;
  5.  
  6. import android.annotation.SuppressLint;
  7. import android.content.Context;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12.  
  13. import com.google.android.gms.maps.CameraUpdate;
  14. import com.google.android.gms.maps.CameraUpdateFactory;
  15. import com.google.android.gms.maps.GoogleMap;
  16. import com.google.android.gms.maps.OnMapReadyCallback;
  17. import com.google.android.gms.maps.SupportMapFragment;
  18. import com.google.android.gms.maps.UiSettings;
  19. import com.google.android.gms.maps.model.LatLng;
  20. import com.google.android.gms.maps.model.MarkerOptions;
  21.  
  22. import android.location.Address;
  23. import android.location.Geocoder;
  24. import android.widget.Button;
  25.  
  26. import org.json.JSONException;
  27. import org.json.JSONObject;
  28.  
  29. import java.io.BufferedReader;
  30. import java.io.IOException;
  31. import java.io.InputStreamReader;
  32. import java.net.HttpURLConnection;
  33. import java.net.URL;
  34. import java.util.List;
  35.  
  36. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  37.  
  38.     private GoogleMap mMap;
  39.     UiSettings mapSettings;
  40.  
  41.     Context context;
  42.     final public String apiUrl= "https://api.darksky.net/forecast/252c95ce53bb1743356e39eb5b4ad506/";
  43.     String url;
  44.     String jsonString;
  45.     String snippet;
  46.     double lat, lng;
  47.     //List<Address> geocodeMatches = null;
  48.  
  49.     double[]latitudes = {40.4378698, 48.8588377, 37.990832, 28.4810971, -23.6821604, 43.7181557};
  50.     double[]longitudes = {-3.8196212, 2.2770201, 23.7033199, -81.5088361, -46.8754942, -79.5181422};
  51.  
  52.     @Override
  53.     protected void onCreate(Bundle savedInstanceState) {
  54.         super.onCreate(savedInstanceState);
  55.         setContentView(R.layout.activity_maps);
  56.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  57.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  58.                 .findFragmentById(R.id.map);
  59.         mapFragment.getMapAsync(this);
  60.         setPointer();
  61.     }
  62.  
  63.     private void setPointer() {
  64.         this.context=this;
  65.         lat = lng = 0;
  66.         getDataJson();
  67.     }
  68.  
  69.     @SuppressLint("StaticFieldLeak")
  70.     private void getDataJson() {
  71.         jsonString = "";
  72.         url = apiUrl + lat + "," + lng;
  73.         Log.i("URL", url);
  74.         new AsyncTask<Void,Void,String>(){
  75.             @Override
  76.             protected String doInBackground(Void... voids) {
  77.                 //we need to open http url connection to our desired url
  78.                 HttpURLConnection connection = null;
  79.  
  80.                 try {
  81.                     //open a connection to http
  82.                     connection = (HttpURLConnection) new URL(url).openConnection();
  83.                     //create new input stream reader
  84.                     InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
  85.                     //open a bufferedReader for getting all the data
  86.                     BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
  87.                     //create an empty line to read
  88.                     String line = "";
  89.                     while ((line=bufferedReader.readLine()) != null) jsonString += line;
  90.                 } catch (IOException e) {
  91.                     e.printStackTrace();
  92.                 } finally {
  93.                     connection.disconnect();
  94.                 }
  95.                 Log.e("data", "doInBackground: \n"+jsonString );
  96.                 return jsonString;
  97.             }
  98.  
  99.             @Override
  100.             protected void onPostExecute(String myString) {
  101.                 try {
  102.                     //create a json object
  103.                     JSONObject jsonObject = new JSONObject(myString);
  104.                     //get the currently object
  105.                     JSONObject currently = jsonObject.getJSONObject("currently");
  106.                     //get status
  107.                     String status = currently.getString("summary");
  108.                     //get the temperature
  109.                     Double temp = currently.getDouble("temperature");
  110.                     int tempC=(int)(((temp-32)*(5/9.0))*100)/100;
  111.                     int hum=(int)(currently.getDouble("humidity")*100);
  112.                     int cloud=(int)(currently.getDouble("cloudCover")*100);
  113.                     snippet = "Hum - " + hum +// "\n" +
  114.                               "; Temp - " + tempC +// "\n" +
  115.                               "; Clouds - " + cloud +// "\n" +
  116.                               "; Status - " + status;// + "\n";
  117.                 } catch (JSONException e) {
  118.                     e.printStackTrace();
  119.                 }
  120.             }
  121.         }.execute();
  122.     }
  123.  
  124.  
  125.     /**
  126.      * Manipulates the map once available.
  127.      * This callback is triggered when the map is ready to be used.
  128.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  129.      * we just add a marker near Sydney, Australia.
  130.      * If Google Play services is not installed on the device, the user will be prompted to install
  131.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  132.      * installed Google Play services and returned to the app.
  133.      */
  134.     @Override
  135.     public void onMapReady(GoogleMap googleMap) {
  136.         mMap = googleMap;
  137.         mapSettings = mMap.getUiSettings();
  138.         mapSettings.setZoomControlsEnabled(true);
  139.         mapSettings.setScrollGesturesEnabled(true);
  140.         mapSettings.setTiltGesturesEnabled(true);
  141.         mapSettings.setRotateGesturesEnabled(true);
  142.     }
  143.  
  144.     public void onClick(View view) {
  145.         switch (view.getId()) {
  146.             case R.id.btnMadrid:
  147.                 getInfo("Madrid, Spain");
  148.                 break;
  149.             case R.id.btnParis:
  150.                 getInfo("Paris, France");
  151.                 break;
  152.             case R.id.btnAthens:
  153.                 getInfo("Athens, Greece");
  154.                 break;
  155.             case R.id.btnOrlando:
  156.                 getInfo("Orlando, FL, USA");
  157.                 break;
  158.             case R.id.btnSaoPaulo:
  159.                 getInfo("Sao Paulo, São Paulo - State of São Paulo, Brazil");
  160.                 break;
  161.             case R.id.btnToronto:
  162.                 getInfo("Toronto, ON, Canada");
  163.                 break;
  164.             default:
  165.                 break;
  166.         }
  167.     }
  168.  
  169.     private void getInfo(String locationName) {
  170.     //    try {
  171.     //        geocodeMatches = new Geocoder(this).getFromLocationName(locationName, 1);
  172.      //   } catch (IOException ioe) {
  173.      //       ioe.printStackTrace();
  174.       //      Log.i("Error","The problem is in " + locationName);
  175.     //    }
  176.      //   if(geocodeMatches != null && !geocodeMatches.isEmpty()) {
  177.     //        lat = geocodeMatches.get(0).getLatitude();
  178.     //        lng = geocodeMatches.get(0).getLongitude();
  179.     //    } else {
  180.             switch (locationName) {
  181.                 case "Madrid, Spain":
  182.                     lat = latitudes[0];
  183.                     lng = longitudes[0];
  184.                     break;
  185.                 case "Paris, France":
  186.                     lat = latitudes[1];
  187.                     lng = longitudes[1];
  188.                     break;
  189.                 case "Athens, Greece":
  190.                     lat = latitudes[2];
  191.                     lng = longitudes[2];
  192.                     break;
  193.                 case "Orlando, FL, USA":
  194.                     lat = latitudes[3];
  195.                     lng = longitudes[3];
  196.                     break;
  197.                 case "Sao Paulo, São Paulo - State of São Paulo, Brazil":
  198.                     lat = latitudes[4];
  199.                     lng = longitudes[4];
  200.                     break;
  201.                 case "Toronto, ON, Canada":
  202.                     lat = latitudes[5];
  203.                     lng = longitudes[5];
  204.                     break;
  205.                 default:
  206.                     break;
  207.             }
  208.     //    }
  209.         LatLng loc = new LatLng(lat, lng);
  210.         getDataJson();
  211.         mMap.addMarker(new MarkerOptions().position(loc).title(locationName).snippet(snippet));
  212.         mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement