daily pastebin goal
55%
SHARE
TWEET

java code for map and fragment

ohmycomputer Jan 19th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ir.ohmycomputer.getlocation;
  2.  
  3. import android.Manifest;
  4. import android.content.Context;
  5. import android.content.pm.PackageManager;
  6. import android.location.Location;
  7. import android.location.LocationManager;
  8. import android.support.annotation.IdRes;
  9. import android.support.annotation.NonNull;
  10. import android.support.annotation.Nullable;
  11. //import android.support.v4.app.ActivityCompat;
  12. import android.support.v4.app.ActivityCompat;
  13. import android.app.Fragment;
  14. import android.app.FragmentManager;
  15. import android.support.v4.app.FragmentActivity;
  16. import android.os.Bundle;
  17. import android.app.FragmentTransaction;
  18. import android.support.v4.content.ContextCompat;
  19. import android.text.Layout;
  20. import android.util.Log;
  21. import android.widget.LinearLayout;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24.  
  25. import com.google.android.gms.common.ConnectionResult;
  26. import com.google.android.gms.common.GoogleApiAvailability;
  27. import com.google.android.gms.common.api.GoogleApiClient;
  28. import com.google.android.gms.common.api.PendingResult;
  29. import com.google.android.gms.location.LocationRequest;
  30. import com.google.android.gms.location.LocationServices;
  31. import com.google.android.gms.location.LocationSettingsRequest;
  32. import com.google.android.gms.location.LocationSettingsResult;
  33. import com.google.android.gms.maps.CameraUpdateFactory;
  34. import com.google.android.gms.maps.GoogleMap;
  35. import com.google.android.gms.maps.OnMapReadyCallback;
  36. import com.google.android.gms.maps.SupportMapFragment;
  37. import com.google.android.gms.maps.model.LatLng;
  38. import com.google.android.gms.maps.model.MarkerOptions;
  39.  
  40. import java.io.FileDescriptor;
  41. import java.io.PrintWriter;
  42. import java.security.Permission;
  43. import java.util.List;
  44.  
  45. public class MapsActivity extends FragmentActivity
  46.         implements OnMapReadyCallback,
  47.         GoogleApiClient.ConnectionCallbacks,
  48.         GoogleApiClient.OnConnectionFailedListener,
  49.         com.google.android.gms.location.LocationListener {
  50.  
  51.     private GoogleMap mMap;
  52.     private GoogleApiClient mGoogleApiClient;
  53.     private Location mLocation;
  54.     private LocationManager locationManager;
  55.     private LocationRequest mLocationRequest;
  56.     double latitude;
  57.     double longitude ;
  58.     /*double longitude;
  59.     double latitude;
  60.     Location location;
  61.     LocationManager lm;*/
  62.     @Override
  63.     protected void onCreate(Bundle savedInstanceState) {
  64.         super.onCreate(savedInstanceState);
  65.         setContentView(R.layout.activity_maps);
  66.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  67.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  68.                 .findFragmentById(R.id.map);
  69.         mapFragment.getMapAsync(this);
  70.  
  71.         int GPSPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
  72.         if (GPSPermission != PackageManager.PERMISSION_GRANTED ) {
  73.             // TODO: Consider calling
  74.             //    ActivityCompat#requestPermissions
  75.             // here to request the missing permissions, and then overriding
  76.             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
  77.             //                                          int[] grantResults)
  78.             // to handle the case where the user grants the permission. See the documentation
  79.             // for ActivityCompat#requestPermissions for more details.}
  80.             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
  81.  
  82.             return;
  83.         }
  84.         mGoogleApiClient = new GoogleApiClient.Builder(this)
  85.                 .addConnectionCallbacks(this)
  86.                 .addOnConnectionFailedListener(this)
  87.                 .addApi(LocationServices.API)
  88.                 .build();
  89.         locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  90.  
  91.  
  92.         //FragmentManager fragmentManager =  getFragmentManager();
  93.         android.support.v4.app.FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction(); //fragmentManager.beginTransaction();
  94.         android.support.v4.app.Fragment suf = new SignUpFrg();
  95.        fragmentTransaction.replace(R.id.signupfrg_holder,suf);
  96.         fragmentTransaction.commit();
  97.  
  98.     }
  99.  
  100.  
  101.  
  102.     /**
  103.      * Manipulates the map once available.
  104.      * This callback is triggered when the map is ready to be used.
  105.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  106.      * we just add a marker near Sydney, Australia.
  107.      * If Google Play services is not installed on the device, the user will be prompted to install
  108.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  109.      * installed Google Play services and returned to the app.
  110.      */
  111.     @Override
  112.     public void onMapReady(GoogleMap googleMap) {
  113.         mMap = googleMap;
  114.         mMap.setMinZoomPreference(14.5f);
  115.         mMap.setMaxZoomPreference(21.0f);
  116.         // Add a marker in Sydney and move the camera
  117.         /*LatLng sydney = new LatLng(-34, 151);
  118.         mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
  119.         mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
  120.  
  121.        //int GPSPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
  122.         //LatLng here = new LatLng(longitude,latitude);
  123.             //LatLng sydney = new LatLng(-34, 151);
  124.            //mMap.addMarker(new MarkerOptions().position(here).title(longitude+"  "+latitude));
  125.        // mMap.moveCamera(CameraUpdateFactory.newLatLng(here));
  126.            /* mMap.addMarker(new MarkerOptions().position(here).title("You're over here"));
  127.             mMap.moveCamera(CameraUpdateFactory.newLatLng(here));*/
  128.     }
  129.  
  130.  
  131.     @Override
  132.     public void onConnected(@Nullable Bundle bundle) {
  133.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  134.             // TODO: Consider calling
  135.             //    ActivityCompat#requestPermissions
  136.             // here to request the missing permissions, and then overriding
  137.             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
  138.             //                                          int[] grantResults)
  139.             // to handle the case where the user grants the permission. See the documentation
  140.             // for ActivityCompat#requestPermissions for more details.
  141.             return;
  142.         } startLocationUpdates();
  143.         mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  144.         if(mLocation == null){
  145.             startLocationUpdates();
  146.         }
  147.         if (mLocation != null) {
  148.              latitude = mLocation.getLatitude();
  149.              longitude = mLocation.getLongitude();
  150.             Log.i("LongLat",latitude + " " + longitude);
  151.             LatLng here = new LatLng(latitude,longitude);
  152.             //LatLng sydney = new LatLng(-34, 151);
  153.             mMap.addMarker(new MarkerOptions().position(here).title(longitude+"  "+latitude));
  154.             mMap.moveCamera(CameraUpdateFactory.newLatLng(here));
  155.         } else {
  156.             // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
  157.         }
  158.     }
  159.  
  160.         protected void startLocationUpdates() {
  161.             // Create the location request
  162.             mLocationRequest = LocationRequest.create()
  163.                     .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
  164.                     .setInterval(1000)
  165.                     .setFastestInterval(1000);
  166.             // Request location updates
  167.             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
  168.                     ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  169.                 // TODO: Consider calling
  170.                 //    ActivityCompat#requestPermissions
  171.                 // here to request the missing permissions, and then overriding
  172.                 //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
  173.                 //                                          int[] grantResults)
  174.                 // to handle the case where the user grants the permission. See the documentation
  175.                 // for ActivityCompat#requestPermissions for more details.
  176.                 return;
  177.             }
  178.             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
  179.                     mLocationRequest, this);
  180.             Log.i("reque", "--->>>>");
  181.         }
  182.  
  183.  
  184.  
  185.     @Override
  186.     public void onConnectionSuspended(int i) {
  187.         Log.i("onConnectionSuspended", "Connection Suspended");
  188.         mGoogleApiClient.connect();
  189.     }
  190.  
  191.     @Override
  192.     public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  193.         Log.i("onConnectionFailed", "Connection failed. Error: " + connectionResult.getErrorCode());
  194.     }
  195.  
  196.     @Override
  197.     public void onLocationChanged(Location location) {
  198.  
  199.     }
  200.  
  201.     @Override
  202.     public void onStart() {
  203.         super.onStart();
  204.         mGoogleApiClient.connect();
  205.     }
  206.  
  207.     @Override
  208.     public void onStop() {
  209.         super.onStop();
  210.         if (mGoogleApiClient.isConnected()) {
  211.             mGoogleApiClient.disconnect();
  212.         }
  213.     }
  214. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top