gpt4 book ai didi

java - 如何从 Google Places API 中删除不需要的地点?

转载 作者:行者123 更新时间:2023-12-01 09:34:59 25 4
gpt4 key购买 nike

我为我的应用程序创建了一个 Google Places API,它目前显示附近的所有地点,如医院、餐馆、学校等。我希望它只显示餐馆。是否可以?如果是这样,怎么办?非常感谢您的帮助。谢谢:)

MapActivity.JAVA

package com.golo.acer.mrestro4;

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 MapActivity extends AppCompatActivity {
private static final int PLACE_PICKER_REQUEST = 1;
private TextView mName;
private TextView mAddress;
private TextView mAttributions;
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
new LatLng(27.689753, 85.311188), new LatLng(27.689753, 85.311188));

@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);
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(BOUNDS_MOUNTAIN_VIEW);
Intent intent = intentBuilder.build(MapActivity.this);
startActivityForResult(intent, PLACE_PICKER_REQUEST);

} catch (GooglePlayServicesRepairableException
| GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
}

public void moveToInternalChoiceActivity(View view) {
Intent intent = new Intent(this, InternalChoiceActivity.class);
startActivity(intent);
}

@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();
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}

mName.setText(name);
mAddress.setText(address);
mAttributions.setText(Html.fromHtml(attributions));

} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

}

最佳答案

place.getPlaceTypes() 返回一个整数列表,如果您想要查找餐厅类型,请检查它是否包含 Place.TYPE_RESTAURANT。您可以在 Place 类中获取以 TYPE_ 开头的类型值。

引用号https://developers.google.com/android/reference/com/google/android/gms/location/places/Place

[编辑]在您的代码中:

final Place place = PlacePicker.getPlace(this, data);
List<Integer> types = place.getPlaceTypes();
for(Integer i : types){
if (i == Place.TYPE_RESTAURANT){
// its restaurant do something
final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}

mName.setText(name);
mAddress.setText(address);
mAttributions.setText(Html.fromHtml(attributions));
}
}

[编辑:(来自用户的评论)]

您所描述的红色图标是一个标记,您可以将它们添加到 map 上。因此,在您的 onActivityResult 中,您会得到一个 Place (已在您的代码中显示)。地点包含名称和位置,使用它在 map 上显示标记,如下所示:

Marker markerPlace = map.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName));

查看 map 标记的文档和相关文章。

现在如何添加 map ?别问我,上网查资料 http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html#mapsview

关于java - 如何从 Google Places API 中删除不需要的地点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39065545/

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