Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
  2. GoogleApiClient.ConnectionCallbacks,
  3. GoogleApiClient.OnConnectionFailedListener,
  4. LocationListener {
  5.  
  6. private GoogleMap mMap;
  7. GoogleApiClient mGoogleApiClient;
  8. Location mLastLocation;
  9. Marker mCurrLocationMarker;
  10. LocationRequest mLocationRequest;
  11. private boolean initiateApp;
  12. double CO2data = 1.02;
  13. double N2data = 0.002;
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_maps);
  19.  
  20. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  21. checkLocationPermission();
  22. }
  23. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  24. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  25. .findFragmentById(R.id.map);
  26. mapFragment.getMapAsync(this);
  27. initiateApp = true;
  28.  
  29.  
  30. }
  31.  
  32.  
  33. /**
  34. * Manipulates the map once available.
  35. * This callback is triggered when the map is ready to be used.
  36. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  37. * we just add a marker near Sydney, Australia.
  38. * If Google Play services is not installed on the device, the user will be prompted to install
  39. * it inside the SupportMapFragment. This method will only be triggered once the user has
  40. * installed Google Play services and returned to the app.
  41. */
  42. @Override
  43. public void onMapReady(GoogleMap googleMap) {
  44. mMap = googleMap;
  45. mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  46.  
  47. //Initialize Google Play Services
  48. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  49. if (ContextCompat.checkSelfPermission(this,
  50. Manifest.permission.ACCESS_FINE_LOCATION)
  51. == PackageManager.PERMISSION_GRANTED) {
  52. buildGoogleApiClient();
  53. mMap.setMyLocationEnabled(true);
  54. }
  55. }
  56. else {
  57. buildGoogleApiClient();
  58. mMap.setMyLocationEnabled(true);
  59. }
  60. }
  61.  
  62. /* Here we create the infoWindow **/
  63. protected synchronized void buildGoogleApiClient() {
  64. mGoogleApiClient = new GoogleApiClient.Builder(this)
  65. .addConnectionCallbacks(this)
  66. .addOnConnectionFailedListener(this)
  67. .addApi(LocationServices.API)
  68. .build();
  69. mGoogleApiClient.connect();
  70.  
  71. mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
  72.  
  73. public View getInfoWindow(Marker arg0) {
  74. View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
  75.  
  76. return v;
  77. }
  78.  
  79. public View getInfoContents(Marker arg0) {
  80.  
  81. //View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
  82.  
  83. return null;
  84.  
  85. }
  86. });
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93. @Override
  94. public void onConnected(Bundle bundle) {
  95.  
  96. mLocationRequest = new LocationRequest();
  97. mLocationRequest.setInterval(0);
  98. mLocationRequest.setFastestInterval(0);
  99. mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  100. if (ContextCompat.checkSelfPermission(this,
  101. Manifest.permission.ACCESS_FINE_LOCATION)
  102. == PackageManager.PERMISSION_GRANTED) {
  103. LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
  104. }
  105.  
  106. }
  107.  
  108. @Override
  109. public void onConnectionSuspended(int i) {
  110.  
  111. }
  112.  
  113. @Override
  114. public void onLocationChanged(Location location) {
  115.  
  116. mLastLocation = location;
  117. if (mCurrLocationMarker != null) {
  118. mCurrLocationMarker.remove();
  119. }
  120.  
  121. //Place current location marker
  122. LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
  123. MarkerOptions markerOptions = new MarkerOptions();
  124. markerOptions.position(latLng);
  125. markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
  126. mCurrLocationMarker = mMap.addMarker(markerOptions);
  127. mCurrLocationMarker.showInfoWindow();
  128.  
  129. Log.d("ADebugTag", "Value: " + Double.toString(location.getLatitude()));
  130. Log.d("ADebugTag", "Value: " + Double.toString(location.getLongitude()));
  131.  
  132.  
  133. //move map camera
  134.  
  135. if(initiateApp){
  136. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
  137. }
  138. initiateApp = false;
  139.  
  140. boolean contains = mMap.getProjection()
  141. .getVisibleRegion()
  142. .latLngBounds
  143. .contains(latLng);
  144.  
  145. if(!contains){
  146. mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  147. }
  148.  
  149.  
  150. }
  151.  
  152. @Override
  153. public void onConnectionFailed(ConnectionResult connectionResult) {
  154.  
  155. }
  156.  
  157. public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
  158. public boolean checkLocationPermission(){
  159. if (ContextCompat.checkSelfPermission(this,
  160. Manifest.permission.ACCESS_FINE_LOCATION)
  161. != PackageManager.PERMISSION_GRANTED) {
  162.  
  163. // Asking user if explanation is needed
  164. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  165. Manifest.permission.ACCESS_FINE_LOCATION)) {
  166.  
  167. // Show an explanation to the user *asynchronously* -- don't block
  168. // this thread waiting for the user's response! After the user
  169. // sees the explanation, try again to request the permission.
  170.  
  171. //Prompt the user once explanation has been shown
  172. ActivityCompat.requestPermissions(this,
  173. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  174. MY_PERMISSIONS_REQUEST_LOCATION);
  175.  
  176.  
  177. } else {
  178. // No explanation needed, we can request the permission.
  179. ActivityCompat.requestPermissions(this,
  180. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  181. MY_PERMISSIONS_REQUEST_LOCATION);
  182. }
  183. return false;
  184. } else {
  185. return true;
  186. }
  187. }
  188.  
  189. @Override
  190. public void onRequestPermissionsResult(int requestCode,
  191. String permissions[], int[] grantResults) {
  192. switch (requestCode) {
  193. case MY_PERMISSIONS_REQUEST_LOCATION: {
  194. // If request is cancelled, the result arrays are empty.
  195. if (grantResults.length > 0
  196. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  197.  
  198. // permission was granted. Do the
  199. // contacts-related task you need to do.
  200. if (ContextCompat.checkSelfPermission(this,
  201. Manifest.permission.ACCESS_FINE_LOCATION)
  202. == PackageManager.PERMISSION_GRANTED) {
  203.  
  204. if (mGoogleApiClient == null) {
  205. buildGoogleApiClient();
  206. }
  207. mMap.setMyLocationEnabled(true);
  208. }
  209.  
  210. } else {
  211.  
  212. // Permission denied, Disable the functionality that depends on this permission.
  213. Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
  214. }
  215. return;
  216. }
  217.  
  218. // other 'case' lines to check for other permissions this app might request.
  219. // You can add here other case statements according to your requirement.
  220. }
  221. }
  222. }
  223.  
  224. <?xml version="1.0" encoding="utf-8"?>
  225. <LinearLayout
  226. xmlns:android="http://schemas.android.com/apk/res/android"
  227. android:layout_width="110dp"
  228. android:layout_height="110dp"
  229. android:orientation="vertical"
  230. android:gravity="center"
  231. >
  232.  
  233. <RelativeLayout
  234. android:layout_width="match_parent"
  235. android:layout_height="match_parent"
  236. >
  237.  
  238. <TextView
  239. android:layout_width="wrap_content"
  240. android:layout_height="wrap_content"
  241. android:id="@+id/infoText"
  242. android:textColor="#000000"
  243. android:padding="10dp"
  244. android:layout_centerInParent="true"
  245. android:background="#ffffff"
  246. />
  247.  
  248. </RelativeLayout>
  249.  
  250. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement