我为我的应用创建了一个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);
}
}
}
答案 0 :(得分:1)
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));
}
}
[编辑:(来自用户的评论)]
您所描述的红色图标是一个标记,您可以将它们添加到地图上。
因此,在onActivityResult
中,您会收到Place
(已在您的代码中显示)。
地方包含名称和位置,使用它在地图上显示标记,如下所示:
Marker markerPlace = map.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName));
查看有关地图标记的文档和相关文章。
现在如何添加地图?不要问我,在互联网上查询信息 http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html#mapsview