Browse Source

crash map

yigit
Yiğit Çolakoğlu 6 years ago
parent
commit
f4432a0e6e
4 changed files with 176 additions and 17 deletions
  1. +3
    -1
      MyCity/app/src/main/AndroidManifest.xml
  2. +168
    -16
      MyCity/app/src/main/java/gq/yigit/mycity/CrashMapFragment.java
  3. +1
    -0
      server_side/api/app.py
  4. +4
    -0
      server_side/api/modules/car_crash.py

+ 3
- 1
MyCity/app/src/main/AndroidManifest.xml View File

@ -40,5 +40,7 @@
</activity> </activity>
</application> </application>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
</manifest> </manifest>

+ 168
- 16
MyCity/app/src/main/java/gq/yigit/mycity/CrashMapFragment.java View File

@ -1,27 +1,33 @@
package gq.yigit.mycity; package gq.yigit.mycity;
import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; 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.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; 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.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 // TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM1 = "param1";
@ -31,12 +37,13 @@ public class CrashMapFragment extends Fragment {
private String mParam1; private String mParam1;
private String mParam2; private String mParam2;
private CrashMapFragment activity;
MapView mMapView; MapView mMapView;
private GoogleMap googleMap; private GoogleMap googleMap;
private String url;
ArrayList<LatLng> markerPoints; ArrayList<LatLng> markerPoints;
private const String API_KEY = "AIzaSyCe3xvqc_FyrPDvu7MptJ3h2GyR1-EpCLw";
private static String API_KEY = "AIzaSyCe3xvqc_FyrPDvu7MptJ3h2GyR1-EpCLw";
private OnFragmentInteractionListener mListener; private OnFragmentInteractionListener mListener;
@ -73,8 +80,83 @@ public class CrashMapFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { 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<LatLng>();
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<String, String>(),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<String> 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 // 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 // TODO: Update argument type and name
void onFragmentInteraction(Uri uri); 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;
}
}
}
} }

+ 1
- 0
server_side/api/app.py View File

@ -50,5 +50,6 @@ if __name__ == '__main__':
api.add_resource(complaint.ComplaintsUpdate,"/complaints_update","/complaints_update/") api.add_resource(complaint.ComplaintsUpdate,"/complaints_update","/complaints_update/")
api.add_resource(car_crash.Crash, '/crash', '/crash/') 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) app.run(host='0.0.0.0', port=5000, ssl_context=context, debug=True)

+ 4
- 0
server_side/api/modules/car_crash.py View File

@ -172,6 +172,10 @@ class Crash(Resource):
return crash return crash
class Crashes(Resource):
def get(self):
return crashes
class Box: class Box:
def __init__(self,coords, type): def __init__(self,coords, type):
self.x1 = coords[0] self.x1 = coords[0]


Loading…
Cancel
Save