Skip to content

Commit

Permalink
#15: Added Volley Request to retrieve data for nearby Privys
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkit4tech committed Jan 8, 2017
1 parent a14375c commit 6da55ae
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 39 deletions.
11 changes: 11 additions & 0 deletions .idea/libraries/gson_2_8_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/libraries/volley_1_0_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand All @@ -35,5 +35,6 @@ dependencies {
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.android.support:multidex:1.0.1'
testCompile 'junit:junit:4.12'

compile 'com.google.code.gson:gson:2.8.0'
compile 'com.android.volley:volley:1.0.0'
}
2 changes: 1 addition & 1 deletion app/src/debug/res/values/google_maps_api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
string in this file.
-->
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">
AIzaSyBRmipbUtS7TF5c9biaFgR7D__JwtELKmk
AIzaSyCBAPcAmX5NUu2LB4fXw8eTd0Zp025nUYY
</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
Expand Down
86 changes: 50 additions & 36 deletions app/src/main/java/com/pulkit4tech/privy/PrivyMapsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.pulkit4tech.privy.data.LocationData;
import com.pulkit4tech.privy.utilities.LocationServices;
import com.pulkit4tech.privy.utilities.RequestData;

import static com.pulkit4tech.privy.constants.Constants.DEBUG;
import static com.pulkit4tech.privy.constants.Constants.CAMERA_ANIMATION_DURATION;
Expand Down Expand Up @@ -64,19 +64,26 @@ protected void onCreate(Bundle savedInstanceState) {
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMapInfo();
addMarkers();
}


private void addMarkers() {

// Add a test marker in Delhi and move the camera
LatLng delhi = new LatLng(28.633011, 77.219373);
mMap.addMarker(new MarkerOptions().position(delhi).anchor(.5f, .5f).title("Marker in Home"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delhi, 15.0f));
if(myLocationData == null) {
LocationServices locationService = new LocationServices(mContext);
myLocationData = locationService.getCurrentLocation();
}

if(myLocationData!=null)
markNearbyPrivys(myLocationData.getLatLng());

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

}

Expand All @@ -87,46 +94,48 @@ private void setUpMapInfo() {
}else {
setUpMyLocationMarker();
}

}

private void setUpMyLocationMarker() {
mMap.setMyLocationEnabled(true);
getMyCurrentLocation();
}

private void getMyCurrentLocation(){
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
LocationServices locationService = new LocationServices(mContext);
myLocationData = locationService.getCurrentLocation();
if(myLocationData!=null) {

// checking for previous marker and if present, replacing it with new marker
if(myLocationMarker!=null){
myLocationMarker.remove();
}

MY_LOCATION_CAMERA_POS = new CameraPosition.Builder()
.target(myLocationData.getLatLng())
.zoom(15.0f)
.bearing(0)
.tilt(25)
.build();

myLocationMarker = mMap.addMarker(new MarkerOptions().position(myLocationData.getLatLng()).title("My Location"));

//animate camera
onGoToMyLocation();
Log.d(DEBUG, myLocationData.getLatLng().toString());
}
getMyCurrentLocation();
return true;
}
});
getMyCurrentLocation();
}

private void getMyCurrentLocation(){
LocationServices locationService = new LocationServices(mContext);
myLocationData = locationService.getCurrentLocation();
if(myLocationData!=null) {

// checking for previous marker and if present, replacing it with new marker
if (myLocationMarker != null) {
myLocationMarker.remove();
}

MY_LOCATION_CAMERA_POS = new CameraPosition.Builder()
.target(myLocationData.getLatLng())
.zoom(15.0f)
.bearing(0)
.tilt(25)
.build();

myLocationMarker = mMap.addMarker(new MarkerOptions().position(myLocationData.getLatLng()).title("My Location"));

//animate camera
moveCameraToMyLocation();
addMarkers();

Log.d(DEBUG, myLocationData.getLatLng().toString());
}
}

private void onGoToMyLocation() {
private void moveCameraToMyLocation() {
changeCamera(CameraUpdateFactory.newCameraPosition(MY_LOCATION_CAMERA_POS), new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
Expand All @@ -138,6 +147,7 @@ public void onCancel() {
Toast.makeText(mContext,"Animation Canceled",Toast.LENGTH_SHORT).show();
}
});

}

private void changeCamera(CameraUpdate update, GoogleMap.CancelableCallback callback){
Expand Down Expand Up @@ -166,4 +176,8 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
Log.d(DEBUG,"Some other request code: " + requestCode);
}
}

private void markNearbyPrivys(LatLng myLocation){
new RequestData(mContext,mMap,myLocation).getMarkerData();
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/pulkit4tech/privy/data/MarkerData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.pulkit4tech.privy.data;

import com.google.android.gms.maps.model.LatLng;

public class MarkerData {

//TODO : This is dummy test => USE GSON to Parse JSON

private LatLng test;

public MarkerData(){
test = new LatLng(28.7037992,77.1006268);
}

public LatLng getTest() {
return test;
}

public void setTest(LatLng test) {
this.test = test;
}
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/pulkit4tech/privy/data/PrivyPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pulkit4tech.privy.data;

public class PrivyPost {

}
91 changes: 91 additions & 0 deletions app/src/main/java/com/pulkit4tech/privy/utilities/RequestData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.pulkit4tech.privy.utilities;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.pulkit4tech.privy.R;
import com.pulkit4tech.privy.data.MarkerData;

import java.util.ArrayList;

import static com.pulkit4tech.privy.constants.Constants.DEBUG;


public class RequestData {

private LatLng myLocation;
private ArrayList<MarkerData> markerData;
private RequestQueue requestQueue;
private Context mContext;
private GoogleMap mMap;

private String LOCATION = "location";
private String NAME_KEY = "name";
private String NAME_VALUE = "toilet";
private String GOOGLE_MAP_API_KEY = "key";
private String MAPS = "maps";
private String API = "api";
private String PLACE = "place";
private String NEARBY = "nearbysearch";
private String TYPE = "json";

public RequestData(Context mContext, GoogleMap mMap, LatLng myLocation){
this.myLocation = myLocation;
this.mContext = mContext;
this.mMap = mMap;
}

public void getMarkerData(){
requestQueue = Volley.newRequestQueue(mContext);
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority(mContext.getResources().getString(R.string.request_api))
.appendPath(MAPS)
.appendPath(API)
.appendPath(PLACE)
.appendPath(NEARBY)
.appendPath(TYPE)
.encodedQuery(LOCATION + "=" +String.format("%f,%f",myLocation.latitude,myLocation.longitude))
.appendQueryParameter(NAME_KEY,NAME_VALUE)
.appendQueryParameter(GOOGLE_MAP_API_KEY,mContext.getResources().getString(R.string.google_maps_key));

final String requestUrl = builder.build().toString();
Log.d(DEBUG,"URL: " + requestUrl);

StringRequest request = new StringRequest(Request.Method.GET, requestUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// testing response
Toast.makeText(mContext,"Got response",Toast.LENGTH_SHORT).show();
Log.d(DEBUG, "Response: " + response);

// dummy data test TODO : Replace with JSON parsing
markerData = new ArrayList<>();
markerData.add(new MarkerData());

for (MarkerData data : markerData){
mMap.addMarker(new MarkerOptions().position(data.getTest()).title("Test"));
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(mContext,"ERROR!!",Toast.LENGTH_SHORT).show();
Log.d(DEBUG, error.toString());
}
});

requestQueue.add(request);
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<resources>
<string name="app_name">Privy</string>
<string name="title_activity_privy_maps">Nearby Privy\'s</string>
<string name="request_api">maps.googleapis.com</string>
</resources>

0 comments on commit 6da55ae

Please sign in to comment.