11package com .example .webapplicationwithspring ;
22import android .*;
33import android .Manifest ;
4+ import android .annotation .SuppressLint ;
45import android .content .pm .PackageManager ;
6+ import android .location .Address ;
7+ import android .location .Geocoder ;
58import android .location .Location ;
69import android .os .Bundle ;
7- import org .jetbrains .annotations .Nullable ;
810
911import androidx .annotation .NonNull ;
12+ import androidx .annotation .Nullable ;
1013import androidx .appcompat .app .AppCompatActivity ;
1114import androidx .core .app .ActivityCompat ;
1215import androidx .core .content .ContextCompat ;
1316
1417import android .util .Log ;
18+ import android .view .KeyEvent ;
19+ import android .view .View ;
20+ import android .view .WindowManager ;
21+ import android .view .inputmethod .EditorInfo ;
22+ import android .widget .AutoCompleteTextView ;
23+ import android .widget .EditText ;
24+ import android .widget .ImageView ;
25+ import android .widget .TextView ;
1526import android .widget .Toast ;
1627
28+ import com .google .android .gms .common .ConnectionResult ;
29+ import com .google .android .gms .common .api .GoogleApiClient ;
1730import com .google .android .gms .location .FusedLocationProviderClient ;
1831import com .google .android .gms .location .LocationServices ;
32+ import com .google .android .gms .location .places .Places ;
1933import com .google .android .gms .maps .CameraUpdateFactory ;
2034import com .google .android .gms .maps .GoogleMap ;
2135import com .google .android .gms .maps .OnMapReadyCallback ;
2236import com .google .android .gms .maps .SupportMapFragment ;
2337import com .google .android .gms .maps .model .LatLng ;
38+ import com .google .android .gms .maps .model .LatLngBounds ;
39+ import com .google .android .gms .maps .model .MarkerOptions ;
2440import com .google .android .gms .tasks .OnCompleteListener ;
2541import com .google .android .gms .tasks .Task ;
2642
27- /**
28- * Created by User on 10/2/2017.
29- */
43+ import java .io .IOException ;
44+ import java .util .ArrayList ;
45+ import java .util .List ;
46+
3047
3148public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
3249
@@ -47,6 +64,7 @@ public void onMapReady(GoogleMap googleMap) {
4764 mMap .setMyLocationEnabled (true );
4865 mMap .getUiSettings ().setMyLocationButtonEnabled (false );
4966
67+ init ();
5068 }
5169 }
5270
@@ -57,6 +75,10 @@ public void onMapReady(GoogleMap googleMap) {
5775 private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234 ;
5876 private static final float DEFAULT_ZOOM = 15f ;
5977
78+ //widgets
79+ private EditText mSearchText ;
80+ private ImageView mGps ;
81+
6082 //vars
6183 private Boolean mLocationPermissionsGranted = false ;
6284 private GoogleMap mMap ;
@@ -66,8 +88,65 @@ public void onMapReady(GoogleMap googleMap) {
6688 protected void onCreate (@ Nullable Bundle savedInstanceState ) {
6789 super .onCreate (savedInstanceState );
6890 setContentView (R .layout .activity_map );
91+ mSearchText = (EditText ) findViewById (R .id .input_search );
92+ mGps = (ImageView ) findViewById (R .id .ic_gps );
6993
7094 getLocationPermission ();
95+
96+ }
97+
98+ private void init (){
99+ Log .d (TAG , "init: initializing" );
100+
101+ mSearchText .setOnEditorActionListener (new TextView .OnEditorActionListener () {
102+ @ Override
103+ public boolean onEditorAction (TextView textView , int actionId , KeyEvent keyEvent ) {
104+ if (actionId == EditorInfo .IME_ACTION_SEARCH
105+ || actionId == EditorInfo .IME_ACTION_DONE
106+ || keyEvent .getAction () == KeyEvent .ACTION_DOWN
107+ || keyEvent .getAction () == KeyEvent .KEYCODE_ENTER ){
108+
109+ //execute our method for searching
110+ geoLocate ();
111+ }
112+
113+ return false ;
114+ }
115+ });
116+
117+ mGps .setOnClickListener (new View .OnClickListener () {
118+ @ Override
119+ public void onClick (View view ) {
120+ Log .d (TAG , "onClick: clicked gps icon" );
121+ getDeviceLocation ();
122+ }
123+ });
124+
125+ hideSoftKeyboard ();
126+ }
127+
128+ private void geoLocate (){
129+ Log .d (TAG , "geoLocate: geolocating" );
130+
131+ String searchString = mSearchText .getText ().toString ();
132+
133+ Geocoder geocoder = new Geocoder (MapActivity .this );
134+ List <Address > list = new ArrayList <>();
135+ try {
136+ list = geocoder .getFromLocationName (searchString , 1 );
137+ }catch (IOException e ){
138+ Log .e (TAG , "geoLocate: IOException: " + e .getMessage () );
139+ }
140+
141+ if (list .size () > 0 ){
142+ Address address = list .get (0 );
143+
144+ Log .d (TAG , "geoLocate: found a location: " + address .toString ());
145+ //Toast.makeText(this, address.toString(), Toast.LENGTH_SHORT).show();
146+
147+ moveCamera (new LatLng (address .getLatitude (), address .getLongitude ()), DEFAULT_ZOOM ,
148+ address .getAddressLine (0 ));
149+ }
71150 }
72151
73152 private void getDeviceLocation (){
@@ -87,7 +166,8 @@ public void onComplete(@NonNull Task task) {
87166 Location currentLocation = (Location ) task .getResult ();
88167
89168 moveCamera (new LatLng (currentLocation .getLatitude (), currentLocation .getLongitude ()),
90- DEFAULT_ZOOM );
169+ DEFAULT_ZOOM ,
170+ "My Location" );
91171
92172 }else {
93173 Log .d (TAG , "onComplete: current location is null" );
@@ -101,9 +181,18 @@ public void onComplete(@NonNull Task task) {
101181 }
102182 }
103183
104- private void moveCamera (LatLng latLng , float zoom ){
184+ private void moveCamera (LatLng latLng , float zoom , String title ){
105185 Log .d (TAG , "moveCamera: moving the camera to: lat: " + latLng .latitude + ", lng: " + latLng .longitude );
106186 mMap .moveCamera (CameraUpdateFactory .newLatLngZoom (latLng , zoom ));
187+
188+ if (!title .equals ("My Location" )){
189+ MarkerOptions options = new MarkerOptions ()
190+ .position (latLng )
191+ .title (title );
192+ mMap .addMarker (options );
193+ }
194+
195+ hideSoftKeyboard ();
107196 }
108197
109198 private void initMap (){
@@ -160,6 +249,8 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
160249 }
161250 }
162251
252+ private void hideSoftKeyboard (){
253+ this .getWindow ().setSoftInputMode (WindowManager .LayoutParams .SOFT_INPUT_STATE_ALWAYS_HIDDEN );
254+ }
163255
164256}
165-
0 commit comments