Skip to content

Commit 6da55ae

Browse files
committed
#15: Added Volley Request to retrieve data for nearby Privys
1 parent a14375c commit 6da55ae

File tree

10 files changed

+197
-39
lines changed

10 files changed

+197
-39
lines changed

.idea/libraries/gson_2_8_0.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/volley_1_0_0.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ android {
2626
}
2727

2828
dependencies {
29-
compile fileTree(dir: 'libs', include: ['*.jar'])
29+
compile fileTree(include: ['*.jar'], dir: 'libs')
3030
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
3131
exclude group: 'com.android.support', module: 'support-annotations'
3232
})
@@ -35,5 +35,6 @@ dependencies {
3535
compile 'com.google.android.gms:play-services:10.0.1'
3636
compile 'com.android.support:multidex:1.0.1'
3737
testCompile 'junit:junit:4.12'
38-
38+
compile 'com.google.code.gson:gson:2.8.0'
39+
compile 'com.android.volley:volley:1.0.0'
3940
}

app/src/debug/res/values/google_maps_api.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
string in this file.
1717
-->
1818
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">
19-
AIzaSyBRmipbUtS7TF5c9biaFgR7D__JwtELKmk
19+
AIzaSyCBAPcAmX5NUu2LB4fXw8eTd0Zp025nUYY
2020
</string>
2121
</resources>

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
location permissions for the 'MyLocation' functionality.
99
-->
1010
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
11+
<uses-permission android:name="android.permission.INTERNET" />
1112

1213
<application
1314
android:allowBackup="true"

app/src/main/java/com/pulkit4tech/privy/PrivyMapsActivity.java

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
import com.google.android.gms.maps.GoogleMap;
1616
import com.google.android.gms.maps.OnMapReadyCallback;
1717
import com.google.android.gms.maps.SupportMapFragment;
18-
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
1918
import com.google.android.gms.maps.model.CameraPosition;
2019
import com.google.android.gms.maps.model.LatLng;
2120
import com.google.android.gms.maps.model.Marker;
2221
import com.google.android.gms.maps.model.MarkerOptions;
2322
import com.pulkit4tech.privy.data.LocationData;
2423
import com.pulkit4tech.privy.utilities.LocationServices;
24+
import com.pulkit4tech.privy.utilities.RequestData;
2525

2626
import static com.pulkit4tech.privy.constants.Constants.DEBUG;
2727
import static com.pulkit4tech.privy.constants.Constants.CAMERA_ANIMATION_DURATION;
@@ -64,19 +64,26 @@ protected void onCreate(Bundle savedInstanceState) {
6464
public void onMapReady(GoogleMap googleMap) {
6565
mMap = googleMap;
6666
setUpMapInfo();
67-
addMarkers();
6867
}
6968

7069

7170
private void addMarkers() {
7271

73-
// Add a test marker in Delhi and move the camera
74-
LatLng delhi = new LatLng(28.633011, 77.219373);
75-
mMap.addMarker(new MarkerOptions().position(delhi).anchor(.5f, .5f).title("Marker in Home"));
76-
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delhi, 15.0f));
72+
if(myLocationData == null) {
73+
LocationServices locationService = new LocationServices(mContext);
74+
myLocationData = locationService.getCurrentLocation();
75+
}
76+
77+
if(myLocationData!=null)
78+
markNearbyPrivys(myLocationData.getLatLng());
7779

78-
LatLng delhi2 = new LatLng(28.633511, 77.219444);
79-
mMap.addMarker(new MarkerOptions().position(delhi2).anchor(.5f, .5f).title("Test Marker in Home2").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
80+
// Add a test marker in Delhi and move the camera
81+
// LatLng delhi = new LatLng(28.633011, 77.219373);
82+
// mMap.addMarker(new MarkerOptions().position(delhi).anchor(.5f, .5f).title("Marker in Home"));
83+
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delhi, 15.0f));
84+
//
85+
// LatLng delhi2 = new LatLng(28.633511, 77.219444);
86+
// mMap.addMarker(new MarkerOptions().position(delhi2).anchor(.5f, .5f).title("Test Marker in Home2").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
8087

8188
}
8289

@@ -87,46 +94,48 @@ private void setUpMapInfo() {
8794
}else {
8895
setUpMyLocationMarker();
8996
}
90-
9197
}
9298

9399
private void setUpMyLocationMarker() {
94100
mMap.setMyLocationEnabled(true);
95-
getMyCurrentLocation();
96-
}
97-
98-
private void getMyCurrentLocation(){
99101
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
100102
@Override
101103
public boolean onMyLocationButtonClick() {
102-
LocationServices locationService = new LocationServices(mContext);
103-
myLocationData = locationService.getCurrentLocation();
104-
if(myLocationData!=null) {
105-
106-
// checking for previous marker and if present, replacing it with new marker
107-
if(myLocationMarker!=null){
108-
myLocationMarker.remove();
109-
}
110-
111-
MY_LOCATION_CAMERA_POS = new CameraPosition.Builder()
112-
.target(myLocationData.getLatLng())
113-
.zoom(15.0f)
114-
.bearing(0)
115-
.tilt(25)
116-
.build();
117-
118-
myLocationMarker = mMap.addMarker(new MarkerOptions().position(myLocationData.getLatLng()).title("My Location"));
119-
120-
//animate camera
121-
onGoToMyLocation();
122-
Log.d(DEBUG, myLocationData.getLatLng().toString());
123-
}
104+
getMyCurrentLocation();
124105
return true;
125106
}
126107
});
108+
getMyCurrentLocation();
109+
}
110+
111+
private void getMyCurrentLocation(){
112+
LocationServices locationService = new LocationServices(mContext);
113+
myLocationData = locationService.getCurrentLocation();
114+
if(myLocationData!=null) {
115+
116+
// checking for previous marker and if present, replacing it with new marker
117+
if (myLocationMarker != null) {
118+
myLocationMarker.remove();
119+
}
120+
121+
MY_LOCATION_CAMERA_POS = new CameraPosition.Builder()
122+
.target(myLocationData.getLatLng())
123+
.zoom(15.0f)
124+
.bearing(0)
125+
.tilt(25)
126+
.build();
127+
128+
myLocationMarker = mMap.addMarker(new MarkerOptions().position(myLocationData.getLatLng()).title("My Location"));
129+
130+
//animate camera
131+
moveCameraToMyLocation();
132+
addMarkers();
133+
134+
Log.d(DEBUG, myLocationData.getLatLng().toString());
135+
}
127136
}
128137

129-
private void onGoToMyLocation() {
138+
private void moveCameraToMyLocation() {
130139
changeCamera(CameraUpdateFactory.newCameraPosition(MY_LOCATION_CAMERA_POS), new GoogleMap.CancelableCallback() {
131140
@Override
132141
public void onFinish() {
@@ -138,6 +147,7 @@ public void onCancel() {
138147
Toast.makeText(mContext,"Animation Canceled",Toast.LENGTH_SHORT).show();
139148
}
140149
});
150+
141151
}
142152

143153
private void changeCamera(CameraUpdate update, GoogleMap.CancelableCallback callback){
@@ -166,4 +176,8 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
166176
Log.d(DEBUG,"Some other request code: " + requestCode);
167177
}
168178
}
179+
180+
private void markNearbyPrivys(LatLng myLocation){
181+
new RequestData(mContext,mMap,myLocation).getMarkerData();
182+
}
169183
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.pulkit4tech.privy.data;
2+
3+
import com.google.android.gms.maps.model.LatLng;
4+
5+
public class MarkerData {
6+
7+
//TODO : This is dummy test => USE GSON to Parse JSON
8+
9+
private LatLng test;
10+
11+
public MarkerData(){
12+
test = new LatLng(28.7037992,77.1006268);
13+
}
14+
15+
public LatLng getTest() {
16+
return test;
17+
}
18+
19+
public void setTest(LatLng test) {
20+
this.test = test;
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.pulkit4tech.privy.data;
2+
3+
public class PrivyPost {
4+
5+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.pulkit4tech.privy.utilities;
2+
3+
import android.content.Context;
4+
import android.net.Uri;
5+
import android.util.Log;
6+
import android.widget.Toast;
7+
8+
import com.android.volley.Request;
9+
import com.android.volley.RequestQueue;
10+
import com.android.volley.Response;
11+
import com.android.volley.VolleyError;
12+
import com.android.volley.toolbox.StringRequest;
13+
import com.android.volley.toolbox.Volley;
14+
import com.google.android.gms.maps.GoogleMap;
15+
import com.google.android.gms.maps.model.LatLng;
16+
import com.google.android.gms.maps.model.MarkerOptions;
17+
import com.pulkit4tech.privy.R;
18+
import com.pulkit4tech.privy.data.MarkerData;
19+
20+
import java.util.ArrayList;
21+
22+
import static com.pulkit4tech.privy.constants.Constants.DEBUG;
23+
24+
25+
public class RequestData {
26+
27+
private LatLng myLocation;
28+
private ArrayList<MarkerData> markerData;
29+
private RequestQueue requestQueue;
30+
private Context mContext;
31+
private GoogleMap mMap;
32+
33+
private String LOCATION = "location";
34+
private String NAME_KEY = "name";
35+
private String NAME_VALUE = "toilet";
36+
private String GOOGLE_MAP_API_KEY = "key";
37+
private String MAPS = "maps";
38+
private String API = "api";
39+
private String PLACE = "place";
40+
private String NEARBY = "nearbysearch";
41+
private String TYPE = "json";
42+
43+
public RequestData(Context mContext, GoogleMap mMap, LatLng myLocation){
44+
this.myLocation = myLocation;
45+
this.mContext = mContext;
46+
this.mMap = mMap;
47+
}
48+
49+
public void getMarkerData(){
50+
requestQueue = Volley.newRequestQueue(mContext);
51+
Uri.Builder builder = new Uri.Builder();
52+
builder.scheme("https")
53+
.authority(mContext.getResources().getString(R.string.request_api))
54+
.appendPath(MAPS)
55+
.appendPath(API)
56+
.appendPath(PLACE)
57+
.appendPath(NEARBY)
58+
.appendPath(TYPE)
59+
.encodedQuery(LOCATION + "=" +String.format("%f,%f",myLocation.latitude,myLocation.longitude))
60+
.appendQueryParameter(NAME_KEY,NAME_VALUE)
61+
.appendQueryParameter(GOOGLE_MAP_API_KEY,mContext.getResources().getString(R.string.google_maps_key));
62+
63+
final String requestUrl = builder.build().toString();
64+
Log.d(DEBUG,"URL: " + requestUrl);
65+
66+
StringRequest request = new StringRequest(Request.Method.GET, requestUrl, new Response.Listener<String>() {
67+
@Override
68+
public void onResponse(String response) {
69+
// testing response
70+
Toast.makeText(mContext,"Got response",Toast.LENGTH_SHORT).show();
71+
Log.d(DEBUG, "Response: " + response);
72+
73+
// dummy data test TODO : Replace with JSON parsing
74+
markerData = new ArrayList<>();
75+
markerData.add(new MarkerData());
76+
77+
for (MarkerData data : markerData){
78+
mMap.addMarker(new MarkerOptions().position(data.getTest()).title("Test"));
79+
}
80+
}
81+
}, new Response.ErrorListener() {
82+
@Override
83+
public void onErrorResponse(VolleyError error) {
84+
Toast.makeText(mContext,"ERROR!!",Toast.LENGTH_SHORT).show();
85+
Log.d(DEBUG, error.toString());
86+
}
87+
});
88+
89+
requestQueue.add(request);
90+
}
91+
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
22
<string name="app_name">Privy</string>
33
<string name="title_activity_privy_maps">Nearby Privy\'s</string>
4+
<string name="request_api">maps.googleapis.com</string>
45
</resources>

0 commit comments

Comments
 (0)