Advertisement
selvalives

Untitled

Dec 11th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. package com.nus.app1;
  2. import android.Manifest;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.pm.PackageManager;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12.  
  13. import androidx.core.app.ActivityCompat;
  14. import androidx.core.content.ContextCompat;
  15.  
  16. import com.google.android.gms.maps.CameraUpdateFactory;
  17. import com.google.android.gms.maps.GoogleMap;
  18. import com.google.android.gms.maps.MapFragment;
  19. import com.google.android.gms.maps.OnMapReadyCallback;
  20. import com.google.android.gms.maps.model.LatLng;
  21. import com.google.android.gms.maps.model.MarkerOptions;
  22.  
  23. public class GoogleMapActivity extends Activity implements OnMapReadyCallback, LocationListener
  24. {
  25. private static final int PERMISSION_REQUEST_CODE1 = 101;
  26. private GoogleMap map;
  27. Button btncurrentlocation,btnlastknownlocation;
  28. LocationManager locationManager;
  29. public void onCreate(Bundle b)
  30. {
  31. super.onCreate(b);
  32. setContentView(R.layout.googlemaplayout);
  33. MapFragment mapFragment = (MapFragment) getFragmentManager()
  34. .findFragmentById(R.id.map);
  35. mapFragment.getMapAsync(this);
  36. btncurrentlocation=findViewById(R.id.btncurrentlocation);
  37. btnlastknownlocation=findViewById(R.id.btnlastknownlocation);
  38. btncurrentlocation.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. getCurrentLocation();
  42. }
  43. });
  44. btnlastknownlocation.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47.  
  48. }
  49. });
  50. }
  51. public void getCurrentLocation()
  52. {
  53. if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
  54. {
  55. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE1);
  56. }
  57. else
  58. {
  59. locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  60. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this);
  61. }
  62. }
  63. public void onRequestPermissionsResult(int requestcode,String[] permissions,int[] grantresults)
  64. {
  65. if(requestcode==PERMISSION_REQUEST_CODE1)
  66. {
  67. if(grantresults.length>0 && grantresults[0]==PackageManager.PERMISSION_GRANTED)
  68. {
  69. getCurrentLocation();
  70. }
  71. }
  72. }
  73. //https://pastebin.com/3ne9n7Qq
  74. @Override
  75. public void onMapReady(GoogleMap googleMap) {
  76. map = googleMap;
  77. LatLng NUS = new LatLng(1.295150, 103.773638);
  78. map.addMarker(new MarkerOptions().position(NUS).title("NUS"));
  79. map.moveCamera(CameraUpdateFactory.newLatLng(NUS));
  80. }
  81.  
  82. @Override
  83. public void onLocationChanged(Location location) {
  84. locationManager.removeUpdates(this);
  85. LatLng currentlocation=new LatLng(location.getLatitude(),location.getLongitude());
  86. map.addMarker(new MarkerOptions().position(currentlocation).title("Current Location"));
  87. map.moveCamera(CameraUpdateFactory.newLatLng(currentlocation));
  88. }
  89.  
  90. @Override
  91. public void onStatusChanged(String provider, int status, Bundle extras) {
  92.  
  93. }
  94.  
  95. @Override
  96. public void onProviderEnabled(String provider) {
  97.  
  98. }
  99.  
  100. @Override
  101. public void onProviderDisabled(String provider) {
  102.  
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement