Guest User

Untitled

a guest
Dec 18th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package com.s0meth1ng.map;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.support.annotation.NonNull;
  6. import android.support.v4.app.ActivityCompat;
  7. import android.support.v4.app.FragmentActivity;
  8. import android.os.Bundle;
  9.  
  10. import com.google.android.gms.maps.CameraUpdateFactory;
  11. import com.google.android.gms.maps.GoogleMap;
  12. import com.google.android.gms.maps.OnMapReadyCallback;
  13. import com.google.android.gms.maps.SupportMapFragment;
  14. import com.google.android.gms.maps.model.LatLng;
  15. import com.google.android.gms.maps.model.MarkerOptions;
  16.  
  17. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  18.  
  19. GoogleMap mMap;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_maps);
  25. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  26. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  27. .findFragmentById(R.id.map);
  28. mapFragment.getMapAsync(this);
  29. }
  30.  
  31.  
  32. /**
  33. * Manipulates the map once available.
  34. * This callback is triggered when the map is ready to be used.
  35. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  36. * we just add a marker near Sydney, Australia.
  37. * If Google Play services is not installed on the device, the user will be prompted to install
  38. * it inside the SupportMapFragment. This method will only be triggered once the user has
  39. * installed Google Play services and returned to the app.
  40. */
  41. @Override
  42. public void onMapReady(GoogleMap map) {
  43. mMap = map;
  44. map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
  45. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  46. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
  47. return;
  48. }
  49. mMap.setMyLocationEnabled(true);
  50. mMap.getUiSettings().setMyLocationButtonEnabled(true);
  51. // Add a marker in Sydney and move the camera
  52. //LatLng sydney = new LatLng(37.421649, -122.083793);
  53. //map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
  54. }
  55.  
  56. @Override
  57. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  58. if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
  59. android.os.Process.killProcess(android.os.Process.myPid());
  60. } else {
  61. //this.recreate();
  62. //Выше костыль вместо того, что ниже (то, что ниже не работает)
  63. this.onMapReady(mMap);
  64. }
  65. }
  66. }
Add Comment
Please, Sign In to add comment