当我第一次运行我的应用程序时,在选择特定的地方类型时,它会生成附近地点的每个地方,例如:atm,商场,食物,学校等。 地方活动代码:
package www.uneditedmap.com.locationandmap;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
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 java.util.List;
import www.uneditedmap.com.locationandmap.model.Places;
import www.uneditedmap.com.locationandmap.parsers.PlacesJSONParser;
public class PlacesActivity extends ListActivity {
TextView placeName;
TextView placeVicinity;
TextView placeStatus;
String locationType;
String myUrl;
ProgressDialog pDialog;
// List<FetchLocationTask> tasks;
List<Places> placesList;
String lat, lng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_places);
// Initialize the TextView for vertical Scrolling
placeName = (TextView) findViewById(R.id.place_title);
placeVicinity = (TextView) findViewById(R.id.vicinity);
placeStatus = (TextView) findViewById(R.id.openNow);
// tasks = new ArrayList<>();
//locationFetcher();
Bundle extras = getIntent().getExtras();
if (extras != null) {
locationType = (extras.getString("TAG")).toString();
lat = extras.getString("lat").toString();
lng = extras.getString("lng").toString();
} else {
// set default value for now
locationType = "atm";
}
myUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="
+ lat + ","
+ lng
+ "&radius="
+ getString(R.string.radius)
+ "&types="
+ locationType
+ "&sensor=true&key="
+ getString(R.string.google_maps_key);
myUrl = myUrl.replaceAll(" ", "_");
//Log.d("generated URL: ", myUrl);
requestData(myUrl);
}
protected void updatePlaces() {
if (placesList.isEmpty()) {
Toast.makeText(getApplicationContext(), "No Result", Toast.LENGTH_LONG).show();
}
PlacesListAdapter adapter = new PlacesListAdapter(this, R.layout.item_vicinity, placesList);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_places, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_refresh) {
//Toast.makeText(this, locationType, Toast.LENGTH_SHORT).show();
requestData(myUrl);
}
return false;
}
private void requestData(String uri) {
/*
Methods for Volley
Comment out this block and Uncomment AsyncTask to Use the AsyncTask block
The Volley Library uses less lines of code than the AsyncTask
*/
StringRequest request = new StringRequest(uri,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
placesList = PlacesJSONParser.parseFeed(response);
updatePlaces();
pDialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError ex) {
//Toast.makeText(PlacesActivity.this, ex.getMessage(), Toast.LENGTH_LONG).show();
Toast.makeText(PlacesActivity.this, "Please Check your network connection and" +
" refresh again", Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
});
// add the request to queue
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
pDialog = new ProgressDialog(PlacesActivity.this);
pDialog.setMessage("Loading locations..");
pDialog.setCancelable(false);
pDialog.show();
//Methods using AsyncTask, Uncomment if using AsyncTask
// FetchLocationTask myTask = new FetchLocationTask();
// myTask.execute(uri);
}
/*
Replacing the AsyncTask with Volley.
To Use the AsyncTask, Comment out the Volley Method inside the RequestData() Method
and Uncomment AsyncTask with the corresponding methods
*/
/*private class FetchLocationTask extends AsyncTask<String, String, List<Places>> {
@Override
protected void onPreExecute() {
//updatePlaces();
pDialog = new ProgressDialog(PlacesActivity.this);
pDialog.setMessage("Loading locations..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected List<Places> doInBackground(String... params) {
String content = HttpManager.getData(params[0]);
placesList = PlacesJSONParser.parseFeed(content);
Log.d("results : ", content);
return placesList;
}
@Override
protected void onPostExecute(List<Places> result) {
if (pDialog.isShowing())
pDialog.dismiss();
if (result == null) {
Toast.makeText(PlacesActivity.this, "Web service not available", Toast.LENGTH_LONG).show();
return;
}
placesList = result;
updatePlaces();
}
@Override
protected void onProgressUpdate(String... values) {
// updatePlaces(values[0]);
}
}*/
}
**Code for Restaurant.java**
package www.uneditedmap.com.locationandmap;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
public class Restaurant extends FragmentActivity {
MyLocation mLocation;
String lat, lng;
String[] keywords;
//String restaurants;
//private int defautltId, restaurantId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_map);
// defautltId = R.drawable.ic_default;
// restaurantId = R.drawable.ic_restaurant;
keywords = getResources().getStringArray(R.array.keywords);
mLocation = new MyLocation(this);
//fetch location method
locationFetcher();
//keywords[0]
if (isOnline()) {
// Toast.makeText(getActivity().getBaseContext(), "You clicked "+ keywords[position]
// + " from " + lat + " , " + lng, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, PlacesActivity.class)
.putExtra("TAG", keywords[0])
.putExtra("lat", lat)
.putExtra("lng", lng);
startActivity(intent);
} else {
Toast.makeText(this.getBaseContext(), "Network Isn't Available", Toast.LENGTH_LONG).show();
}
}
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
public void locationFetcher() {
if (mLocation.canGetLocation()) {
//mLocation.getLocation();
double latitude = mLocation.getLatitude();
double longitude = mLocation.getLongitude();
lat = String.valueOf(latitude);
lng = String.valueOf(longitude);
} else {
Toast.makeText(this, "LOCATION NOT ACQUIRED,TURN ON A PROVIDER", Toast.LENGTH_SHORT).show();
}
}
}
将card.java首次添加到cardview堆栈的cardview代码:
package www.uneditedmap.com.locationandmap;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by null on 28/02/2015.
*/
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
public static String FAV = "New Activity";
List<EndangeredItem> mItems;
// LayoutInflater inflater;
private Context context;
public GridAdapter() {
super();
//inflater = LayoutInflater.from(viewGroup.getContext())
// this.context=context;
// this.mItems=mItems;
// inflater = LayoutInflater.from(context);
mItems = new ArrayList<EndangeredItem>();
EndangeredItem species = new EndangeredItem();
species.setName("Restaurant");
species.setThumbnail(R.drawable.restaurant);
mItems.add(species);
species = new EndangeredItem();
species.setName("Churches");
species.setThumbnail(R.drawable.church);
mItems.add(species);
species = new EndangeredItem();
species.setName("Laundry");
species.setThumbnail(R.drawable.laundry);
mItems.add(species);
species = new EndangeredItem();
species.setName("Schools");
species.setThumbnail(R.drawable.school);
mItems.add(species);
species = new EndangeredItem();
species.setName("Cinemas");
species.setThumbnail(R.drawable.cinema);
mItems.add(species);
species = new EndangeredItem();
species.setName("Hospitals");
species.setThumbnail(R.drawable.hospital);
mItems.add(species);
species = new EndangeredItem();
species.setName("Tech Centers");
species.setThumbnail(R.drawable.hubs);
mItems.add(species);
species = new EndangeredItem();
species.setName("Clubs");
species.setThumbnail(R.drawable.clubs);
mItems.add(species);
species = new EndangeredItem();
species.setName("Fashion House");
species.setThumbnail(R.drawable.fashion);
mItems.add(species);
species = new EndangeredItem();
species.setName("Banks");
species.setThumbnail(R.drawable.bank);
mItems.add(species);
species = new EndangeredItem();
species.setName("Atms");
species.setThumbnail(R.drawable.atm);
mItems.add(species);
species = new EndangeredItem();
species.setName("Sport Centers");
species.setThumbnail(R.drawable.sportcenters);
mItems.add(species);
species = new EndangeredItem();
species.setName("Supermarkets");
species.setThumbnail(R.drawable.supermarket);
mItems.add(species);
species = new EndangeredItem();
species.setName("Sitouts");
species.setThumbnail(R.drawable.sitouts);
mItems.add(species);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.grid_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
EndangeredItem nature = mItems.get(i);
viewHolder.tvspecies.setText(nature.getName());
viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public ImageView imgThumbnail;
public TextView tvspecies;
public ViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
imgThumbnail = (ImageView) itemView.findViewById(R.id.img_thumbnail);
tvspecies = (TextView) itemView.findViewById(R.id.tv_species);
imgThumbnail.setOnClickListener(this);
//to enable user clicking long n the cardview image
imgThumbnail.setOnLongClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = null;
switch (getPosition()) {
case 0:
intent = new Intent(context, Restaurant.class);
break;
case 1:
intent = new Intent(context, TestIntent.class);
break;
case 2:
intent = new Intent(context, TestIntent.class);
break;
case 3:
intent = new Intent(context, TestIntent.class);
break;
case 4:
intent = new Intent(context, TestIntent.class);
break;
case 5:
intent = new Intent(context, TestIntent.class);
break;
case 6:
intent = new Intent(context, TestIntent.class);
break;
default:
intent = new Intent(context, MainActivity.class);
break;
}
context.startActivity(intent);
}
@Override
public boolean onLongClick(View view) {
// Intent intent;
// intent = new Intent(context, Favourites.class);
// intent.putExtra(FAV, R.id.img_thumbnail);
// context.startActivity(intent);
Toast.makeText(context, "added to favourites", Toast.LENGTH_SHORT).show();
return false;
}
}
}
我的字符串数组:
<string-array name="keywords">
<item>Restaurants</item>
<item>Churches</item>
<item>Laundry</item>
<item>Schools</item>
<item>Cinemas</item>
<item>Hospitals</item>
<item>Tech Centers</item>
<item>Clubs</item>
<item>Fashion House</item>
<item>Banks</item>
<item>Atms</item>
<item>Sport Centers</item>
<item>Supermarkets</item>
<item>Sitouts</item>
</string-array>
答案 0 :(得分:1)
使用此Google API获取位置周围的食物和酒吧
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=ADD_LATITUDE,ADD_LONGITUDE&radius=5000&types=food,bar&sensor=true&key=YOUR_GOOGLE_MAP_BROWSER_KEY
在AndroidManifest.xml
:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
还添加此权限:
<uses-permission android:name="android.permission.INTERNET" />
<强>响应:强>
在浏览器中输入网址时,您将获得 JSON响应。
答案 1 :(得分:0)
将此用作搜索特定位置的URL字符串。
字符串loginURL =&#34; https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=19.9974,73.7876113&radius=2000&type=SBH&name=atm&key=API_KEY_HERE&#34 ;;
答案 2 :(得分:0)
问题是您的附近搜索请求包含types=Restaurants
,但这不是types
参数的有效值。有效值在Place Types文档的表1中。例如。而不是Restaurants
,而不是restaurant
。
第二个问题是types
是不推荐使用的参数。您一次只传递一种类型,因此您可以轻松切换到完全支持的type
参数。请参阅Place Search文档中的弃用通知。