gpt4 book ai didi

android - Nearby Places 搜索给出随机结果而不是特定结果

转载 作者:行者123 更新时间:2023-11-30 01:44:21 26 4
gpt4 key购买 nike

当我第一次运行我的应用程序时,在点击特定地点类型时,它会生成附近地点的每个类型,例如:在我指定的英里半径内的 atm、商场、食品、学校等。地点 Activity 代码:

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();
}

}

}

首先将 restaurant.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>

最佳答案

使用此 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
  • Google Developers Console 启用 GooglePlacesAPI .
  • 生成浏览器 key 不是 Android 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" />

响应:

当您在浏览器中输入 URL 时,您将得到 JSON 响应

关于android - Nearby Places 搜索给出随机结果而不是特定结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33950468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com