Skip to content

Commit 3f75b8c

Browse files
committed
Map with some features.
1 parent c098f64 commit 3f75b8c

File tree

14 files changed

+187
-8
lines changed

14 files changed

+187
-8
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ dependencies {
4343
implementation 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
4444
// implementation 'com.google.android.gms:play-services-maps:17.0.0'
4545
implementation 'com.google.android.gms:play-services:12.0.1'
46+
// implementation 'com.google..android.libraries.places:places:2.2.0'
4647
}

app/src/main/java/com/example/webapplicationwithspring/MapActivity.java

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,49 @@
11
package com.example.webapplicationwithspring;
22
import android.*;
33
import android.Manifest;
4+
import android.annotation.SuppressLint;
45
import android.content.pm.PackageManager;
6+
import android.location.Address;
7+
import android.location.Geocoder;
58
import android.location.Location;
69
import android.os.Bundle;
7-
import org.jetbrains.annotations.Nullable;
810

911
import androidx.annotation.NonNull;
12+
import androidx.annotation.Nullable;
1013
import androidx.appcompat.app.AppCompatActivity;
1114
import androidx.core.app.ActivityCompat;
1215
import androidx.core.content.ContextCompat;
1316

1417
import 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;
1526
import android.widget.Toast;
1627

28+
import com.google.android.gms.common.ConnectionResult;
29+
import com.google.android.gms.common.api.GoogleApiClient;
1730
import com.google.android.gms.location.FusedLocationProviderClient;
1831
import com.google.android.gms.location.LocationServices;
32+
import com.google.android.gms.location.places.Places;
1933
import com.google.android.gms.maps.CameraUpdateFactory;
2034
import com.google.android.gms.maps.GoogleMap;
2135
import com.google.android.gms.maps.OnMapReadyCallback;
2236
import com.google.android.gms.maps.SupportMapFragment;
2337
import 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;
2440
import com.google.android.gms.tasks.OnCompleteListener;
2541
import 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

3148
public 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-
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24"
6+
android:tint="#010203"
7+
android:alpha="0.8">
8+
<group android:scaleX="1.1111112"
9+
android:scaleY="1.1111112"
10+
android:translateX="-1.3333334"
11+
android:translateY="-1.3333334">
12+
<path
13+
android:fillColor="@android:color/white"
14+
android:pathData="M12,8c-2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4zM20.94,11c-0.46,-4.17 -3.77,-7.48 -7.94,-7.94L13,1h-2v2.06C6.83,3.52 3.52,6.83 3.06,11L1,11v2h2.06c0.46,4.17 3.77,7.48 7.94,7.94L11,23h2v-2.06c4.17,-0.46 7.48,-3.77 7.94,-7.94L23,13v-2h-2.06zM12,19c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z"/>
15+
</group>
16+
</vector>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24"
6+
android:tint="#010203"
7+
android:alpha="0.8">
8+
<group android:scaleX="1.25"
9+
android:scaleY="1.25"
10+
android:translateX="-3"
11+
android:translateY="-3">
12+
<path
13+
android:fillColor="@android:color/white"
14+
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
15+
</group>
16+
</vector>
555 Bytes
Loading
478 Bytes
Loading
341 Bytes
Loading
348 Bytes
Loading
683 Bytes
Loading
599 Bytes
Loading

0 commit comments

Comments
 (0)