Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ir.ohmycomputer.getlocation;
- import android.Manifest;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationManager;
- import android.support.annotation.IdRes;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- //import android.support.v4.app.ActivityCompat;
- import android.support.v4.app.ActivityCompat;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.support.v4.app.FragmentActivity;
- import android.os.Bundle;
- import android.app.FragmentTransaction;
- import android.support.v4.content.ContextCompat;
- import android.text.Layout;
- import android.util.Log;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.GoogleApiAvailability;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.api.PendingResult;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.location.LocationSettingsRequest;
- import com.google.android.gms.location.LocationSettingsResult;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- import java.io.FileDescriptor;
- import java.io.PrintWriter;
- import java.security.Permission;
- import java.util.List;
- public class MapsActivity extends FragmentActivity
- implements OnMapReadyCallback,
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- com.google.android.gms.location.LocationListener {
- private GoogleMap mMap;
- private GoogleApiClient mGoogleApiClient;
- private Location mLocation;
- private LocationManager locationManager;
- private LocationRequest mLocationRequest;
- double latitude;
- double longitude ;
- /*double longitude;
- double latitude;
- Location location;
- LocationManager lm;*/
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- int GPSPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
- if (GPSPermission != PackageManager.PERMISSION_GRANTED ) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.}
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
- return;
- }
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- //FragmentManager fragmentManager = getFragmentManager();
- android.support.v4.app.FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction(); //fragmentManager.beginTransaction();
- android.support.v4.app.Fragment suf = new SignUpFrg();
- fragmentTransaction.replace(R.id.signupfrg_holder,suf);
- fragmentTransaction.commit();
- }
- /**
- * Manipulates the map once available.
- * This callback is triggered when the map is ready to be used.
- * This is where we can add markers or lines, add listeners or move the camera. In this case,
- * we just add a marker near Sydney, Australia.
- * If Google Play services is not installed on the device, the user will be prompted to install
- * it inside the SupportMapFragment. This method will only be triggered once the user has
- * installed Google Play services and returned to the app.
- */
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- mMap.setMinZoomPreference(14.5f);
- mMap.setMaxZoomPreference(21.0f);
- // Add a marker in Sydney and move the camera
- /*LatLng sydney = new LatLng(-34, 151);
- mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
- //int GPSPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
- //LatLng here = new LatLng(longitude,latitude);
- //LatLng sydney = new LatLng(-34, 151);
- //mMap.addMarker(new MarkerOptions().position(here).title(longitude+" "+latitude));
- // mMap.moveCamera(CameraUpdateFactory.newLatLng(here));
- /* mMap.addMarker(new MarkerOptions().position(here).title("You're over here"));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(here));*/
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.
- return;
- } startLocationUpdates();
- mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
- if(mLocation == null){
- startLocationUpdates();
- }
- if (mLocation != null) {
- latitude = mLocation.getLatitude();
- longitude = mLocation.getLongitude();
- Log.i("LongLat",latitude + " " + longitude);
- LatLng here = new LatLng(latitude,longitude);
- //LatLng sydney = new LatLng(-34, 151);
- mMap.addMarker(new MarkerOptions().position(here).title(longitude+" "+latitude));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(here));
- } else {
- // Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
- }
- }
- protected void startLocationUpdates() {
- // Create the location request
- mLocationRequest = LocationRequest.create()
- .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
- .setInterval(1000)
- .setFastestInterval(1000);
- // Request location updates
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
- ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.
- return;
- }
- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
- mLocationRequest, this);
- Log.i("reque", "--->>>>");
- }
- @Override
- public void onConnectionSuspended(int i) {
- Log.i("onConnectionSuspended", "Connection Suspended");
- mGoogleApiClient.connect();
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- Log.i("onConnectionFailed", "Connection failed. Error: " + connectionResult.getErrorCode());
- }
- @Override
- public void onLocationChanged(Location location) {
- }
- @Override
- public void onStart() {
- super.onStart();
- mGoogleApiClient.connect();
- }
- @Override
- public void onStop() {
- super.onStop();
- if (mGoogleApiClient.isConnected()) {
- mGoogleApiClient.disconnect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement