Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. package com.abc.psproject;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.pm.PackageManager;
  7. import android.location.Location;
  8. import android.location.LocationListener;
  9. import android.location.LocationManager;
  10. import android.os.Bundle;
  11. import android.support.v4.app.ActivityCompat;
  12. import android.support.v4.app.FragmentActivity;
  13. import android.support.v4.content.ContextCompat;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. import com.google.android.gms.maps.CameraUpdateFactory;
  20. import com.google.android.gms.maps.GoogleMap;
  21. import com.google.android.gms.maps.MapFragment;
  22. import com.google.android.gms.maps.OnMapReadyCallback;
  23. import com.google.android.gms.maps.model.LatLng;
  24. import com.google.android.gms.maps.model.MarkerOptions;
  25.  
  26. /**
  27. * Created by selva on 12/20/2017.
  28. */
  29.  
  30. public class googlemapactivity extends FragmentActivity implements OnMapReadyCallback,LocationListener
  31. {
  32. private GoogleMap map;
  33. private TextView tcurrentlocation;
  34. private TextView tlastknownlocation;
  35. private Button btncurrentlocation;
  36. private Button btnlastknownlocation;
  37. private LocationManager locationmanager;
  38. public void onCreate(Bundle b)
  39. {
  40. super.onCreate(b);
  41. setContentView(R.layout.googlemaplayout);
  42. MapFragment mapFragment=(MapFragment)getFragmentManager().findFragmentById(R.id.mapfragment);
  43. mapFragment.getMapAsync(this);
  44. tcurrentlocation=findViewById(R.id.tcurrentlocation);
  45. tlastknownlocation=findViewById(R.id.tlastknownlocation);
  46. btncurrentlocation=findViewById(R.id.btncurrentlocation);
  47. btncurrentlocation.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View view) {
  50. tcurrentlocation.setText("Loading Current Location ....");
  51. getcurrentlocation();;
  52. }
  53. });
  54. btnlastknownlocation=findViewById(R.id.btnlastknownlocation);
  55. btnlastknownlocation.setOnClickListener(new View.OnClickListener() {
  56. @Override
  57. public void onClick(View view) {
  58. tlastknownlocation.setText("Loading Last Known Location....");
  59. getlastknownlocation();
  60. }
  61. });
  62. }
  63.  
  64. @Override
  65. public void onMapReady(GoogleMap googleMap)
  66. {
  67. map=googleMap;
  68. LatLng kismec=new LatLng(3.855542, 103.320440);
  69. map.addMarker(new MarkerOptions().position(kismec).title("Pahang Skills"));
  70. map.moveCamera(CameraUpdateFactory.newLatLng(kismec));
  71. }
  72. public void getcurrentlocation()
  73. {
  74. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
  75. {
  76. ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},1);
  77. }
  78. else
  79. {
  80. locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  81. locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,googlemapactivity.this);
  82. }
  83. }
  84. public void getlastknownlocation()
  85. {
  86. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
  87. {
  88. ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_COARSE_LOCATION},2);
  89. }
  90. else
  91. {
  92. locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  93. Location location=locationmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  94. tlastknownlocation.setText(location.getLatitude()+","+location.getLongitude());
  95. LatLng lastknownlocation=new LatLng(location.getLatitude(),location.getLongitude());
  96. map.addMarker(new MarkerOptions().position(lastknownlocation).title("Last Known Location"));
  97. map.moveCamera(CameraUpdateFactory.newLatLng(lastknownlocation));
  98. }
  99. }
  100. public void onRequestPermissionsResult(int requestcode,String[] permissions,int[] grantresults)
  101. {
  102. if(requestcode==1)
  103. {
  104. if(grantresults.length>0 && grantresults[0]==PackageManager.PERMISSION_GRANTED)
  105. {
  106. getcurrentlocation();
  107. }
  108. else
  109. {
  110. Toast.makeText(getApplicationContext(),"You denied location permission",Toast.LENGTH_LONG).show();
  111. }
  112. }
  113. else if(requestcode==2)
  114. {
  115. if(grantresults.length>0 && grantresults[0]==PackageManager.PERMISSION_GRANTED)
  116. {
  117. getlastknownlocation();
  118. }
  119. else
  120. {
  121. Toast.makeText(getApplicationContext(),"You denied location permission",Toast.LENGTH_LONG).show();
  122. }
  123. }
  124. }
  125. @Override
  126. public void onLocationChanged(Location location)
  127. {
  128. tcurrentlocation.setText(location.getLatitude()+","+location.getLongitude());
  129. locationmanager.removeUpdates(this);
  130. LatLng currentlocation=new LatLng(location.getLatitude(),location.getLongitude());
  131. map.addMarker(new MarkerOptions().position(currentlocation).title("Current Location"));
  132. map.moveCamera(CameraUpdateFactory.newLatLng(currentlocation));
  133.  
  134. }
  135. @Override
  136. public void onStatusChanged(String s, int i, Bundle bundle)
  137. {}
  138. @Override
  139. public void onProviderEnabled(String s)
  140. {}
  141. @Override
  142. public void onProviderDisabled(String s)
  143. {}
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement