Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sgl.cityviewer;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.fragment.app.FragmentActivity;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import com.google.android.gms.maps.CameraUpdate;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.UiSettings;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- import android.location.Address;
- import android.location.Geocoder;
- import android.widget.Button;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.List;
- public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
- private GoogleMap mMap;
- UiSettings mapSettings;
- Context context;
- final public String apiUrl= "https://api.darksky.net/forecast/252c95ce53bb1743356e39eb5b4ad506/";
- String url;
- String jsonString;
- String snippet;
- double lat, lng;
- //List<Address> geocodeMatches = null;
- double[]latitudes = {40.4378698, 48.8588377, 37.990832, 28.4810971, -23.6821604, 43.7181557};
- double[]longitudes = {-3.8196212, 2.2770201, 23.7033199, -81.5088361, -46.8754942, -79.5181422};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- setPointer();
- }
- private void setPointer() {
- this.context=this;
- lat = lng = 0;
- getDataJson();
- }
- @SuppressLint("StaticFieldLeak")
- private void getDataJson() {
- jsonString = "";
- url = apiUrl + lat + "," + lng;
- Log.i("URL", url);
- new AsyncTask<Void,Void,String>(){
- @Override
- protected String doInBackground(Void... voids) {
- //we need to open http url connection to our desired url
- HttpURLConnection connection = null;
- try {
- //open a connection to http
- connection = (HttpURLConnection) new URL(url).openConnection();
- //create new input stream reader
- InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
- //open a bufferedReader for getting all the data
- BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
- //create an empty line to read
- String line = "";
- while ((line=bufferedReader.readLine()) != null) jsonString += line;
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- connection.disconnect();
- }
- Log.e("data", "doInBackground: \n"+jsonString );
- return jsonString;
- }
- @Override
- protected void onPostExecute(String myString) {
- try {
- //create a json object
- JSONObject jsonObject = new JSONObject(myString);
- //get the currently object
- JSONObject currently = jsonObject.getJSONObject("currently");
- //get status
- String status = currently.getString("summary");
- //get the temperature
- Double temp = currently.getDouble("temperature");
- int tempC=(int)(((temp-32)*(5/9.0))*100)/100;
- int hum=(int)(currently.getDouble("humidity")*100);
- int cloud=(int)(currently.getDouble("cloudCover")*100);
- snippet = "Hum - " + hum +// "\n" +
- "; Temp - " + tempC +// "\n" +
- "; Clouds - " + cloud +// "\n" +
- "; Status - " + status;// + "\n";
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }.execute();
- }
- /**
- * Manipulates the map once available.
- * This callback is triggered when the map is ready to be used.
- * This is where we can add markers or lines, add listeners or move the camera. In this case,
- * we just add a marker near Sydney, Australia.
- * If Google Play services is not installed on the device, the user will be prompted to install
- * it inside the SupportMapFragment. This method will only be triggered once the user has
- * installed Google Play services and returned to the app.
- */
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- mapSettings = mMap.getUiSettings();
- mapSettings.setZoomControlsEnabled(true);
- mapSettings.setScrollGesturesEnabled(true);
- mapSettings.setTiltGesturesEnabled(true);
- mapSettings.setRotateGesturesEnabled(true);
- }
- public void onClick(View view) {
- switch (view.getId()) {
- case R.id.btnMadrid:
- getInfo("Madrid, Spain");
- break;
- case R.id.btnParis:
- getInfo("Paris, France");
- break;
- case R.id.btnAthens:
- getInfo("Athens, Greece");
- break;
- case R.id.btnOrlando:
- getInfo("Orlando, FL, USA");
- break;
- case R.id.btnSaoPaulo:
- getInfo("Sao Paulo, São Paulo - State of São Paulo, Brazil");
- break;
- case R.id.btnToronto:
- getInfo("Toronto, ON, Canada");
- break;
- default:
- break;
- }
- }
- private void getInfo(String locationName) {
- // try {
- // geocodeMatches = new Geocoder(this).getFromLocationName(locationName, 1);
- // } catch (IOException ioe) {
- // ioe.printStackTrace();
- // Log.i("Error","The problem is in " + locationName);
- // }
- // if(geocodeMatches != null && !geocodeMatches.isEmpty()) {
- // lat = geocodeMatches.get(0).getLatitude();
- // lng = geocodeMatches.get(0).getLongitude();
- // } else {
- switch (locationName) {
- case "Madrid, Spain":
- lat = latitudes[0];
- lng = longitudes[0];
- break;
- case "Paris, France":
- lat = latitudes[1];
- lng = longitudes[1];
- break;
- case "Athens, Greece":
- lat = latitudes[2];
- lng = longitudes[2];
- break;
- case "Orlando, FL, USA":
- lat = latitudes[3];
- lng = longitudes[3];
- break;
- case "Sao Paulo, São Paulo - State of São Paulo, Brazil":
- lat = latitudes[4];
- lng = longitudes[4];
- break;
- case "Toronto, ON, Canada":
- lat = latitudes[5];
- lng = longitudes[5];
- break;
- default:
- break;
- }
- // }
- LatLng loc = new LatLng(lat, lng);
- getDataJson();
- mMap.addMarker(new MarkerOptions().position(loc).title(locationName).snippet(snippet));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement