From f4432a0e6e69128444a9d9111e304c0b74feee4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Tue, 18 Jun 2019 14:44:26 +0300 Subject: [PATCH] crash map --- MyCity/app/src/main/AndroidManifest.xml | 4 +- .../gq/yigit/mycity/CrashMapFragment.java | 184 ++++++++++++++++-- server_side/api/app.py | 1 + server_side/api/modules/car_crash.py | 4 + 4 files changed, 176 insertions(+), 17 deletions(-) diff --git a/MyCity/app/src/main/AndroidManifest.xml b/MyCity/app/src/main/AndroidManifest.xml index 6574e58..332dac4 100644 --- a/MyCity/app/src/main/AndroidManifest.xml +++ b/MyCity/app/src/main/AndroidManifest.xml @@ -40,5 +40,7 @@ - + \ No newline at end of file diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/CrashMapFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/CrashMapFragment.java index 2d684f5..62c0879 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/CrashMapFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/CrashMapFragment.java @@ -1,27 +1,33 @@ package gq.yigit.mycity; +import android.Manifest; import android.content.Context; +import android.content.DialogInterface; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; +import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; +import android.support.v4.content.ContextCompat; +import android.support.v7.app.AlertDialog; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import com.google.android.gms.maps.MapView; -import com.google.android.gms.maps.model.LatLng; - import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + +import com.google.android.gms.maps.*; +import com.google.android.gms.maps.model.*; +import gq.yigit.mycity.tools.FileActions; +import gq.yigit.mycity.tools.WebRequest; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; -/** - * A simple {@link Fragment} subclass. - * Activities that contain this fragment must implement the - * {@link CrashMapFragment.OnFragmentInteractionListener} interface - * to handle interaction events. - * Use the {@link CrashMapFragment#newInstance} factory method to - * create an instance of this fragment. - */ -public class CrashMapFragment extends Fragment { +public class CrashMapFragment extends Fragment implements WebRequest.responseListener { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; @@ -31,12 +37,13 @@ public class CrashMapFragment extends Fragment { private String mParam1; private String mParam2; + private CrashMapFragment activity; MapView mMapView; private GoogleMap googleMap; - + private String url; ArrayList markerPoints; - private const String API_KEY = "AIzaSyCe3xvqc_FyrPDvu7MptJ3h2GyR1-EpCLw"; + private static String API_KEY = "AIzaSyCe3xvqc_FyrPDvu7MptJ3h2GyR1-EpCLw"; private OnFragmentInteractionListener mListener; @@ -73,8 +80,83 @@ public class CrashMapFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_crash_map, container, false); + View rootView = inflater.inflate(R.layout.fragment_crash_map, container, false); + + mMapView= (MapView) rootView.findViewById(R.id.mapView); + mMapView.onCreate(savedInstanceState); + + mMapView.onResume(); + activity = this; + + try { + MapsInitializer.initialize(getActivity().getApplicationContext()); + } catch (Exception e) { + e.printStackTrace(); + } + + mMapView.getMapAsync(new OnMapReadyCallback() { + @Override + public void onMapReady(GoogleMap mMap) { + googleMap = mMap; + + if (checkLocationPermission()) { + if (ContextCompat.checkSelfPermission(getActivity(), + Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + googleMap.setMyLocationEnabled(true); + } + } + markerPoints = new ArrayList(); + + googleMap.getUiSettings().setCompassEnabled(true); + googleMap.getUiSettings().setMyLocationButtonEnabled(true); + googleMap.getUiSettings().setRotateGesturesEnabled(true); + + FileActions file_manager = new FileActions(); + url = file_manager.readFromFile(getContext(),"server.config").trim(); + String url_crashes = url + "/crashes"; + WebRequest data_request = new WebRequest(url_crashes,true,new HashMap(),0); + data_request.addListener(activity); + data_request.execute(); + + CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(39.925533,32.866287)).zoom(13).build(); + googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); + } + }); + + return rootView; + } + + public void receivedResponse(boolean success,String response, int reqid){ + if(success){ + try{ + JSONObject crashes = new JSONObject(response); + Iterator iter = crashes.keys(); + while (iter.hasNext()) { + String key = iter.next(); + try { + JSONArray c = crashes.getJSONArray(key); + for (int i = 0 ; i < c.length(); i++) { + JSONObject crash = c.getJSONObject(i); + JSONObject location = crash.getJSONObject("location"); + String lat = location.getString("latitude"); + String lng = location.getString("longitude"); + + String priority = crash.getString("priority"); + String message = crash.getString("message"); + + LatLng ankara = new LatLng(Float.parseFloat(lat),Float.parseFloat(lng)); + googleMap.addMarker(new MarkerOptions().position(ankara). + title("Priority " + priority).snippet(message)); + } + } catch (JSONException e) { + // Something went wrong! + } + } + }catch (Exception e){ + Log.e("[ERROR]",e.getMessage()); + } + } } // TODO: Rename method, update argument and hook method into UI event @@ -115,4 +197,74 @@ public class CrashMapFragment extends Fragment { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } + + public boolean checkLocationPermission() { + if (ContextCompat.checkSelfPermission(getActivity(), + Manifest.permission.ACCESS_FINE_LOCATION) + != PackageManager.PERMISSION_GRANTED) { + + // Should we show an explanation? + if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), + Manifest.permission.ACCESS_FINE_LOCATION)) { + + // Show an explanation to the user *asynchronously* -- don't block + // this thread waiting for the user's response! After the user + // sees the explanation, try again to request the permission. + new AlertDialog.Builder(getActivity()) + .setTitle("") + .setMessage("") + .setPositiveButton("Ok", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + //Prompt the user once explanation has been shown + ActivityCompat.requestPermissions(getActivity(),new String[] + {Manifest.permission.ACCESS_FINE_LOCATION},1); + } + }) + .create() + .show(); + + + } else { + // No explanation needed, we can request the permission. + ActivityCompat.requestPermissions(getActivity(), + new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, + 1); + } + return false; + } else { + return true; + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, + String permissions[], int[] grantResults) { + switch (requestCode) { + case 1: + { + // If request is cancelled, the result arrays are empty. + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + + // permission was granted, yay! Do the + // location-related task you need to do. + if (ContextCompat.checkSelfPermission(getActivity(), + Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + + + googleMap.setMyLocationEnabled(true); + } + + } else { + + + + } + return; + } + + } + } } diff --git a/server_side/api/app.py b/server_side/api/app.py index 64130a3..413346c 100644 --- a/server_side/api/app.py +++ b/server_side/api/app.py @@ -50,5 +50,6 @@ if __name__ == '__main__': api.add_resource(complaint.ComplaintsUpdate,"/complaints_update","/complaints_update/") api.add_resource(car_crash.Crash, '/crash', '/crash/') + api.add_resource(car_crash.Crashes, '/crashes', '/crashes/') app.run(host='0.0.0.0', port=5000, ssl_context=context, debug=True) diff --git a/server_side/api/modules/car_crash.py b/server_side/api/modules/car_crash.py index 4156c3a..6ce116d 100644 --- a/server_side/api/modules/car_crash.py +++ b/server_side/api/modules/car_crash.py @@ -172,6 +172,10 @@ class Crash(Resource): return crash +class Crashes(Resource): + def get(self): + return crashes + class Box: def __init__(self,coords, type): self.x1 = coords[0]