|
| 1 | +package com.lenovo.memorableplaces; |
| 2 | + |
| 3 | +import android.*; |
| 4 | +import android.Manifest; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.pm.PackageManager; |
| 8 | +import android.location.Address; |
| 9 | +import android.location.Geocoder; |
| 10 | +import android.location.Location; |
| 11 | +import android.location.LocationListener; |
| 12 | +import android.location.LocationManager; |
| 13 | +import android.os.Build; |
| 14 | +import android.support.annotation.NonNull; |
| 15 | +import android.support.v4.app.ActivityCompat; |
| 16 | +import android.support.v4.app.FragmentActivity; |
| 17 | +import android.os.Bundle; |
| 18 | +import android.support.v4.content.ContextCompat; |
| 19 | +import android.widget.Toast; |
| 20 | + |
| 21 | +import com.google.android.gms.maps.CameraUpdateFactory; |
| 22 | +import com.google.android.gms.maps.GoogleMap; |
| 23 | +import com.google.android.gms.maps.OnMapReadyCallback; |
| 24 | +import com.google.android.gms.maps.SupportMapFragment; |
| 25 | +import com.google.android.gms.maps.model.LatLng; |
| 26 | +import com.google.android.gms.maps.model.MarkerOptions; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.text.SimpleDateFormat; |
| 30 | +import java.util.Date; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Locale; |
| 33 | + |
| 34 | +public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener { |
| 35 | + |
| 36 | + private GoogleMap mMap; |
| 37 | + |
| 38 | + LocationManager locationManager; |
| 39 | + |
| 40 | + LocationListener locationListener; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 44 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 45 | + |
| 46 | + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 47 | + |
| 48 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
| 49 | + |
| 50 | + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); |
| 51 | + |
| 52 | + Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
| 53 | + |
| 54 | + centerMapOnLocation(lastKnownLocation, "Your location"); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + public void centerMapOnLocation(Location location, String title) { |
| 64 | + |
| 65 | + LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude()); |
| 66 | + |
| 67 | + mMap.clear(); |
| 68 | + |
| 69 | + if (title != "Your location") { |
| 70 | + |
| 71 | + mMap.addMarker(new MarkerOptions().position(userLocation).title(title)); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 10)); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void onCreate(Bundle savedInstanceState) { |
| 82 | + super.onCreate(savedInstanceState); |
| 83 | + setContentView(R.layout.activity_maps); |
| 84 | + // Obtain the SupportMapFragment and get notified when the map is ready to be used. |
| 85 | + SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() |
| 86 | + .findFragmentById(R.id.map); |
| 87 | + mapFragment.getMapAsync(this); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * Manipulates the map once available. |
| 93 | + * This callback is triggered when the map is ready to be used. |
| 94 | + * This is where we can add markers or lines, add listeners or move the camera. In this case, |
| 95 | + * we just add a marker near Sydney, Australia. |
| 96 | + * If Google Play services is not installed on the device, the user will be prompted to install |
| 97 | + * it inside the SupportMapFragment. This method will only be triggered once the user has |
| 98 | + * installed Google Play services and returned to the app. |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public void onMapReady(GoogleMap googleMap) { |
| 102 | + mMap = googleMap; |
| 103 | + |
| 104 | + mMap.setOnMapLongClickListener(this); |
| 105 | + |
| 106 | + Intent intent = getIntent(); |
| 107 | + |
| 108 | + if (intent.getIntExtra("placeNumber", 0) == 0) { |
| 109 | + |
| 110 | + // zoom in on user's location |
| 111 | + |
| 112 | + locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); |
| 113 | + |
| 114 | + locationListener = new LocationListener() { |
| 115 | + @Override |
| 116 | + public void onLocationChanged(Location location) { |
| 117 | + |
| 118 | + centerMapOnLocation(location, "Your location"); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onStatusChanged(String s, int i, Bundle bundle) { |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onProviderEnabled(String s) { |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onProviderDisabled(String s) { |
| 134 | + |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + if (Build.VERSION.SDK_INT < 23) { |
| 139 | + |
| 140 | + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); |
| 141 | + |
| 142 | + } else { |
| 143 | + |
| 144 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { |
| 145 | + |
| 146 | + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); |
| 147 | + |
| 148 | + Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
| 149 | + |
| 150 | + centerMapOnLocation(lastKnownLocation, "Your location"); |
| 151 | + |
| 152 | + } else { |
| 153 | + |
| 154 | + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + } else { |
| 163 | + |
| 164 | + Location placeLocation = new Location(LocationManager.GPS_PROVIDER); |
| 165 | + placeLocation.setLatitude(MainActivity.locations.get(intent.getIntExtra("placeNumber", 0)).latitude); |
| 166 | + placeLocation.setLongitude(MainActivity.locations.get(intent.getIntExtra("placeNumber", 0)).longitude); |
| 167 | + |
| 168 | + centerMapOnLocation(placeLocation, MainActivity.places.get(intent.getIntExtra("placeNumber", 0))); |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void onMapLongClick(LatLng latLng) { |
| 176 | + |
| 177 | + Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); |
| 178 | + |
| 179 | + String address = ""; |
| 180 | + |
| 181 | + try { |
| 182 | + |
| 183 | + List<Address> listAddresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); |
| 184 | + |
| 185 | + if (listAddresses != null && listAddresses.size() > 0) { |
| 186 | + |
| 187 | + if (listAddresses.get(0).getThoroughfare() != null) { |
| 188 | + |
| 189 | + if (listAddresses.get(0).getSubThoroughfare() != null) { |
| 190 | + |
| 191 | + address += listAddresses.get(0).getSubThoroughfare() + " "; |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + address += listAddresses.get(0).getThoroughfare(); |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + } catch (IOException e) { |
| 202 | + e.printStackTrace(); |
| 203 | + } |
| 204 | + |
| 205 | + if (address == "") { |
| 206 | + |
| 207 | + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm yyyy-MM-dd"); |
| 208 | + |
| 209 | + address = sdf.format(new Date()); |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + mMap.addMarker(new MarkerOptions().position(latLng).title(address)); |
| 214 | + |
| 215 | + MainActivity.places.add(address); |
| 216 | + MainActivity.locations.add(latLng); |
| 217 | + |
| 218 | + MainActivity.arrayAdapter.notifyDataSetChanged(); |
| 219 | + |
| 220 | + Toast.makeText(this, "Location Saved", Toast.LENGTH_SHORT).show(); |
| 221 | + |
| 222 | + } |
| 223 | +} |
0 commit comments