Java Google places API返回单一类型;汽车“修理”;
我正在使用GooglePlacesAPI,它将返回我所在位置的所有位置。然而,我只希望它返回一个类型的“汽车维修”,我想我几乎有它,但我错过了一些东西,如果有人可以引导我在正确的方向,这将是伟大的:) 到目前为止我的代码 普氏活性Java Google places API返回单一类型;汽车“修理”;,java,android-studio,google-places-api,Java,Android Studio,Google Places Api,我正在使用GooglePlacesAPI,它将返回我所在位置的所有位置。然而,我只希望它返回一个类型的“汽车维修”,我想我几乎有它,但我错过了一些东西,如果有人可以引导我在正确的方向,这将是伟大的:) 到目前为止我的代码 普氏活性 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
public class PlacePickerActivity extends AppCompatActivity {
private static final int PLACE_PICKER_REQUEST = 1;
String url="https://maps.googleapis.com/maps/api/place/textsearch/json?type=car_repair&key=AIzaSyBKsTtLyMBQH8mhvbknJ4MvZwACotmeYO0";
private TextView mName;
private TextView mAddress;
private TextView mAttributions;
private TextView mNumber;
private static final LatLngBounds Sligo = new LatLngBounds(
new LatLng(54.27, -8.47), new LatLng(54.27, -8.47));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_picker);
mName = (TextView) findViewById(R.id.textView);
mAddress = (TextView) findViewById(R.id.textView2);
mAttributions = (TextView) findViewById(R.id.textView3);
mNumber = (TextView) findViewById(R.id.textView4);
Button pickerButton = (Button) findViewById(R.id.pickerButton);
pickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
intentBuilder.setLatLngBounds(Sligo);
Intent intent = intentBuilder.build(PlacePickerActivity.this);
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException
| GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST
&& resultCode == Activity.RESULT_OK) {
final Place place = PlacePicker.getPlace(this, data);
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
final CharSequence formatted_phone_number = place.getPhoneNumber();
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}
mName.setText(name);
mAddress.setText(address);
mAttributions.setText(Html.fromHtml(attributions));
mNumber.setText(formatted_phone_number);
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.truiton.placepicker">
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBKsTtLyMBQH8mhvbknJ4MvZwACotmeYO0"/>
<activity
android:name=".PlacePickerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
尝试类似于以下内容的查询:
位置的格式是纬度、经度……请注意“类型”不是类型
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=54.27,-8.47&radius=1000&types=car_repair&key=AddYourOwnKeyHere
请尝试使用类似以下内容的查询:
位置的格式是纬度、经度……请注意“类型”不是类型
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=54.27,-8.47&radius=1000&types=car_repair&key=AddYourOwnKeyHere
因为此API返回范围内的位置类型。有一种方法可以改善结果,但仍然不是最优的,称为setLatLngBounds
我以前试过这个API,发现它在某种程度上完全是一团糟。
例如,如果您希望在大学内查找餐厅,此API不仅会返回餐厅,还会返回许多其他类型,如大学
。设置一个边界可能很好,但如果只使用API,仍然会得到恼人的结果。
如果您只需要汽车维修类型
,我建议您编写一个过滤器来过滤其他类型,并且只保留汽车维修类型和相关信息。,因为此API将位置类型返回到范围内。有一种方法可以改善结果,但仍然不是最优的,称为setLatLngBounds
我以前试过这个API,发现它在某种程度上完全是一团糟。
例如,如果您希望在大学内查找餐厅,此API不仅会返回餐厅,还会返回许多其他类型,如大学
。设置一个边界可能很好,但如果只使用API,仍然会得到恼人的结果。
如果您只需要汽车维修类型
,我建议您编写一个过滤器来过滤其他类型,只保留汽车维修类型和相关信息。我已经为您准备好了这个,希望对您有所帮助
// A class to store your results
public class Place {
private String icon;
private Double latitude;
public void setIcon(String icon) {
this.icon=icon;
}
public void setLatitude(Double latitude) {
this.latitude=latitude;
}
// .....
}
// Utility class to keep things simple
public class Functions {
public static JSONObject convertInputStreamToJSONObject(InputStream inputStream)
throws JSONException, IOException
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return new JSONObject(result);
}
public static ArrayList<Place> parsePlacesFromJson(JSONObject datos) throws JSONException {
List<Place> placesList = new ArrayList<>();
// in your case this must be "results" I think
final int LIST_LENGTH = datos.getJSONArray("results").length();
//final int LIST_LENGTH = datos.getJSONArray("nameOfTheJsonMainArray").length();
Log.d("Length", String.valueOf(LIST_LENGTH));
// For every element in the list
for(int i = 0; i < LIST_LENGTH; i++) {
// Instance of a new Place
Place place = new Place();
// Get data as needed, this represents one place
JSONObject obj = datos.getJSONArray("results").getJSONObject(i);
Double latitude = obj.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
String icon = obj.getString("icon");
place.setLatitude(latitude);
place.setIcon(icon);
//....
placesList.add(place)
}
return placesList;
}
}
// The google places reader class
public class ApiReader {
private static final String TAG = "API_READER";
Context context;
View v;
public ApiReader(Context context, View v) {
this.context = context;
this.v = v;
}
private static final String APIKEY = "yourkeyhere";
private static String ENDPOINT =
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=54.27,-8.47&radius=1000&types=car_repair&key=%s";
// Method to be called
public void getCarRepairs() throws IOException {
URL url = new URL(String.format(ENDPOINT, APIKEY));
new GetPlaces().execute(url);
}
// Inner asyctask class
private class GetPlaces extends AsyncTask<URL, Integer, List<Place>> {
List<Place> lista;
@Override
protected List<Place> doInBackground(URL... params) {
JSONObject datos;
URL url = params[0];
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
datos = Functions.convertInputStreamToJSONObject(in);
Log.i(TAG, datos.toString());
// De los datos obtenemos un objeto Place
lista = Functions.parsePlacesFromJson(datos);
Log.i(TAG, "" + lista.toString());
Log.i(TAG, "Went into try was OK.");
} catch (Exception e) {
Log.e(TAG, e.toString());
Log.i(TAG, "Went into catch");
} finally {
urlConnection.disconnect();
Log.i(TAG, "Went into finally, urlConnection disconnected.");
}
return lista;
}
// This method it gets what the "doInBackGround" returns
protected void onPostExecute(List<Place> placesList) {
if(placesList != null) {
// Do whatever with the list of places
//Functions.updateView(v, placesList);
}
}
}
}
// And to make things work in yout activity just do like this
ApiReader api = new ApiReader(this, findViewById(R.id.mainLayout));
try {
api.getCarRepairs();
} catch (IOException e) {
e.printStackTrace();
}
//用于存储结果的类
公共课场所{
私有字符串图标;
私人双纬度;
公共无效设置图标(字符串图标){
this.icon=icon;
}
公共纬度(双纬度){
这个。纬度=纬度;
}
// .....
}
//实用程序类使事情保持简单
公共类功能{
公共静态JSONObject convertInputStreamToJSONObject(InputStream InputStream)
抛出JSONException、IOException
{
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
字符串行=”;
字符串结果=”;
而((line=bufferedReader.readLine())!=null)
结果+=行;
inputStream.close();
返回新的JSONObject(结果);
}
公共静态ArrayList parsePlacesFromJson(JSONObject datos)抛出JSONException{
List placesList=new ArrayList();
//在你的情况下,我认为这一定是“结果”
final int LIST_LENGTH=datos.getJSONArray(“结果”).LENGTH();
//final int LIST_LENGTH=datos.getJSONArray(“JsonMainArray的名称”).LENGTH();
Log.d(“长度”,String.valueOf(LIST_Length));
//对于列表中的每个元素
对于(int i=0;i