Created
June 7, 2014 12:00
-
-
Save ceosilvajr/e1c3b9b4cdb88f2cc80e to your computer and use it in GitHub Desktop.
SAMPLE SERVER GOOGLE API KEY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.silva.fragments; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.LinearLayout; | |
import android.widget.ListView; | |
import android.widget.ProgressBar; | |
import android.widget.Spinner; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.actionbarsherlock.app.SherlockFragment; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import com.koushikdutta.async.future.FutureCallback; | |
import com.koushikdutta.ion.Ion; | |
import com.quickroadassistance.android.R; | |
import com.silva.adapters.NearbyAdapter; | |
import com.silva.managers.UserLocationManager; | |
import com.silva.objects.Establishment; | |
import com.silva.objects.UserLocation; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by ceosi_000 on 3/31/2014. | |
*/ | |
public class HelpFragment extends SherlockFragment { | |
private final static String TAG = "HELP FRAGMENT"; | |
private String apiKey; | |
private Context mContext; | |
private ArrayAdapter<CharSequence> spinnerAdapter; | |
private UserLocation userLocation; | |
private Spinner spinner; | |
private Button search; | |
private String type; | |
private LinearLayout lyNoResultsFound; | |
private TextView txvErrorMessage; | |
private ArrayAdapter<Establishment> stablishmentAdapter; | |
private ProgressBar pbStablishment; | |
private ListView lstStablishment; | |
private List<Establishment> establishmentList; | |
private boolean nearbyAdapterHasData; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
mContext = getSherlockActivity(); | |
Ion.getDefault(mContext).configure().setLogging(TAG, Log.DEBUG); | |
establishmentList = new ArrayList<Establishment>(); | |
apiKey = getString(R.string.server_api_key); | |
nearbyAdapterHasData = false; | |
View view = inflater.inflate(R.layout.container_help, container, false); | |
lstStablishment = (ListView) view.findViewById(R.id.help_authority_list); | |
pbStablishment = (ProgressBar) view.findViewById(R.id.help_progress); | |
spinner = (Spinner) view.findViewById(R.id.help_spinner); | |
search = (Button) view.findViewById(R.id.help_search); | |
lyNoResultsFound = (LinearLayout) view.findViewById(R.id.help_no_result_found); | |
txvErrorMessage = (TextView) view.findViewById(R.id.help_error_message); | |
search.setOnClickListener(searchClicked); | |
spinnerAdapter = ArrayAdapter.createFromResource(mContext, R.array.help_dropdown_list, | |
android.R.layout.simple_spinner_item); | |
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
spinner.setAdapter(spinnerAdapter); | |
spinner.setOnItemSelectedListener(spinnerListener); | |
return view; | |
} | |
private View.OnClickListener searchClicked = new View.OnClickListener() { | |
private String url; | |
private String base_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="; | |
@Override | |
public void onClick(View v) { | |
Log.i(TAG, "ITEM SELECTED " + type); | |
lstStablishment.setVisibility(View.GONE); | |
userLocation = UserLocationManager.getUserLocationData(mContext); | |
if (nearbyAdapterHasData) { | |
stablishmentAdapter.clear(); | |
stablishmentAdapter.notifyDataSetChanged(); | |
} | |
double latitude = userLocation.getLatitude(); | |
double longitude = userLocation.getLongitude(); | |
if (type == null) { | |
Toast.makeText(mContext, getString(R.string.choose_one), Toast.LENGTH_LONG).show(); | |
} else { | |
url = base_url + latitude + "," + longitude + "&radius=500&types=" + type + "&sensor=true&key=" | |
+ apiKey; | |
System.out.println(url); | |
pbStablishment.setVisibility(View.VISIBLE); | |
lyNoResultsFound.setVisibility(View.GONE); | |
Ion.with(mContext, url).asJsonObject().setCallback(new FutureCallback<JsonObject>() { | |
@Override | |
public void onCompleted(Exception e, JsonObject json) { | |
if (e != null) { | |
lstStablishment.setVisibility(View.GONE); | |
pbStablishment.setVisibility(View.GONE); | |
lyNoResultsFound.setVisibility(View.VISIBLE); | |
txvErrorMessage.setText(getString(R.string.no_internet_connection)); | |
} else { | |
if (json.get("status").getAsString().equals("OK")) { | |
JsonArray results = json.get("results").getAsJsonArray(); | |
if (results.size() > 0) { | |
for (int i = 0; i < results.size(); i++) { | |
JsonObject jo = results.get(i).getAsJsonObject(); | |
Establishment establishment = new Establishment(jo); | |
establishmentList.add(establishment); | |
} | |
stablishmentAdapter = new NearbyAdapter(mContext, establishmentList); | |
nearbyAdapterHasData = true; | |
lstStablishment.setAdapter(stablishmentAdapter); | |
pbStablishment.setVisibility(View.GONE); | |
lstStablishment.setVisibility(View.VISIBLE); | |
} | |
} else if (json.get("status").getAsString().equals("ZERO_RESULTS")) { | |
lstStablishment.setVisibility(View.GONE); | |
pbStablishment.setVisibility(View.GONE); | |
lyNoResultsFound.setVisibility(View.VISIBLE); | |
txvErrorMessage.setText(getString(R.string.no_result_found)); | |
} else { | |
System.out.println("LOG ERROR"); | |
lstStablishment.setVisibility(View.GONE); | |
pbStablishment.setVisibility(View.GONE); | |
lyNoResultsFound.setVisibility(View.VISIBLE); | |
txvErrorMessage.setText(getString(R.string.internal_error)); | |
} | |
} | |
} | |
}); | |
} | |
} | |
}; | |
private AdapterView.OnItemSelectedListener spinnerListener = new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
switch (position) { | |
case 0: | |
type = null; | |
break; | |
case 1: | |
type = "car_repair"; | |
break; | |
case 2: | |
type = "car_wash"; | |
break; | |
case 3: | |
type = "fire_station"; | |
break; | |
case 4: | |
type = "gas_station"; | |
break; | |
case 5: | |
type = "hardware_store"; | |
break; | |
case 6: | |
type = "hospital"; | |
break; | |
case 7: | |
type = "insurance_agency"; | |
break; | |
case 8: | |
type = "parking"; | |
break; | |
case 9: | |
type = "police"; | |
break; | |
default: | |
type = null; | |
break; | |
} | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
// TODO Auto-generated method stub | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment