|
| 1 | +package com.lenovo.userlocationdemo; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.pm.PackageManager; |
| 6 | +import android.location.Address; |
| 7 | +import android.location.Geocoder; |
| 8 | +import android.location.Location; |
| 9 | +import android.location.LocationListener; |
| 10 | +import android.location.LocationManager; |
| 11 | +import android.os.Build; |
| 12 | +import android.support.annotation.NonNull; |
| 13 | +import android.support.v4.app.ActivityCompat; |
| 14 | +import android.support.v4.app.FragmentActivity; |
| 15 | +import android.os.Bundle; |
| 16 | +import android.support.v4.content.ContextCompat; |
| 17 | +import android.util.Log; |
| 18 | + |
| 19 | +import com.google.android.gms.maps.CameraUpdateFactory; |
| 20 | +import com.google.android.gms.maps.GoogleMap; |
| 21 | +import com.google.android.gms.maps.OnMapReadyCallback; |
| 22 | +import com.google.android.gms.maps.SupportMapFragment; |
| 23 | +import com.google.android.gms.maps.model.LatLng; |
| 24 | +import com.google.android.gms.maps.model.MarkerOptions; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Locale; |
| 29 | + |
| 30 | +public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { |
| 31 | + |
| 32 | + private GoogleMap mMap; |
| 33 | + LocationManager locationManager; |
| 34 | + LocationListener locationListener; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 38 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 39 | + |
| 40 | + if(requestCode ==1) { |
| 41 | + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 42 | + if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
| 43 | + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void onCreate(Bundle savedInstanceState) { |
| 51 | + super.onCreate(savedInstanceState); |
| 52 | + setContentView(R.layout.activity_maps); |
| 53 | + // Obtain the SupportMapFragment and get notified when the map is ready to be used. |
| 54 | + SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() |
| 55 | + .findFragmentById(R.id.map); |
| 56 | + mapFragment.getMapAsync(this); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Manipulates the map once available. |
| 63 | + * This callback is triggered when the map is ready to be used. |
| 64 | + * This is where we can add markers or lines, add listeners or move the camera. In this case, |
| 65 | + * we just add a marker near Sydney, Australia. |
| 66 | + * If Google Play services is not installed on the device, the user will be prompted to install |
| 67 | + * it inside the SupportMapFragment. This method will only be triggered once the user has |
| 68 | + * installed Google Play services and returned to the app. |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public void onMapReady(GoogleMap googleMap) { |
| 72 | + mMap = googleMap; |
| 73 | + |
| 74 | + // Add a marker in Sydney and move the camera |
| 75 | + |
| 76 | + |
| 77 | + locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); |
| 78 | + locationListener = new LocationListener() { |
| 79 | + @Override |
| 80 | + public void onLocationChanged(Location location) { |
| 81 | + //Toast.makeText(MapsActivity.this, location.toString(),Toast.LENGTH_SHORT).show(); |
| 82 | + LatLng userlocation = new LatLng(location.getLatitude(), location.getLongitude()); |
| 83 | + mMap.clear(); |
| 84 | + mMap.addMarker(new MarkerOptions().position(userlocation).title("Your Location")); |
| 85 | + mMap.moveCamera(CameraUpdateFactory.newLatLng(userlocation)); |
| 86 | + Geocoder geocoder = new Geocoder(getApplicationContext(),Locale.getDefault()); |
| 87 | + |
| 88 | + try { |
| 89 | + List<Address> listaddress = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1); |
| 90 | + if(listaddress != null && listaddress.size()>0) |
| 91 | + { |
| 92 | + Log.i("PlaceInfo",listaddress.get(0).toString()); |
| 93 | + } |
| 94 | + } catch (IOException e) { |
| 95 | + e.printStackTrace(); |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void onStatusChanged(String provider, int status, Bundle extras) { |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onProviderEnabled(String provider) { |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void onProviderDisabled(String provider) { |
| 112 | + |
| 113 | + } |
| 114 | + }; |
| 115 | + if (Build.VERSION.SDK_INT < 23) { |
| 116 | + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); |
| 117 | + } else { |
| 118 | + if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
| 119 | + //ask for permission |
| 120 | + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); |
| 121 | + } else { //we have permission |
| 122 | + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); |
| 123 | + Location lastknownlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
| 124 | + LatLng userlocation = new LatLng(lastknownlocation.getLatitude(), lastknownlocation.getLongitude()); |
| 125 | + mMap.clear(); |
| 126 | + mMap.addMarker(new MarkerOptions().position(userlocation).title("Your Location")); |
| 127 | + mMap.moveCamera(CameraUpdateFactory.newLatLng(userlocation)); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments