如何通过Google Places Autocomplete搜索地图自定义终端标记
问题:自定义终端标记无法通过Google Places Autocomplete搜索到
我在Google Maps中添加了多个自定义终端标记,想通过Places Autocomplete功能搜索这些标记,但尝试多种方法都没实现。请问是否需要借助地理定位,或是有其他可行方案?希望能得到相关建议与思路。
现有自动补全功能代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); String apiKey = getString(R.string.places_api_key); if (!Places.isInitialized()) { Places.initialize(getApplicationContext(), apiKey); } PlacesClient placesClient = Places.createClient(this); // Initialize the AutocompleteSupportFragment. AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment); autocompleteFragment.setLocationBias(RectangularBounds.newInstance( new LatLng(15.147181, 120.584733), new LatLng(15.147181, 120.584733) )); //Select Country autocompleteFragment.setCountries("PH"); // Specify the types of place data to return. autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG)); // Set up a PlaceSelectionListener to handle the response. autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { @Override public void onPlaceSelected(@NonNull Place place) { // TODO: Get info about the selected place. Log.i(TAG, "Place: " + place.getName() + ", " + place.getId()); // Get the LatLng of the selected place LatLng selectedPlaceLatLng = place.getLatLng(); // Define a new CameraPosition with desired zoom level CameraPosition cameraPosition = new CameraPosition.Builder() .target(selectedPlaceLatLng) // Sets the center of the map to the selected place's LatLng .zoom(15) // Desired zoom level .build(); // Move the camera to the new position mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); } @Override public void onError(@NonNull Status status) { // TODO: Handle the error. Log.i(TAG, "An error occurred: " + status); } }); }
自定义标记添加代码
// Add some markers to the map, and add a data object to each marker. markerMarisolTerm = mMap.addMarker(new MarkerOptions() .position(MarisolTerminal) .title("Marisol Terminal") .flat(true) .icon(BitmapDescriptorFactory.fromResource(R.drawable.marisol))); markerMarisolTerm.setTag(0); markerVillaTerminal = mMap.addMarker(new MarkerOptions() .position(VillaTerminal) .title("Villa Terminal") .flat(true) .icon(BitmapDescriptorFactory.fromResource(R.drawable.villa))); markerVillaTerminal.setTag(0); markerCheckpointTerminal = mMap.addMarker(new MarkerOptions() .position(CheckpointTerminal) .title("Checkpoint Terminal") .flat(true) .icon(BitmapDescriptorFactory.fromResource(R.drawable.checkpoint))); markerCheckpointTerminal.setTag(0);
可行解决方案思路
1. 问题核心
你添加的自定义标记属于本地自定义地点,不在Google Places官方数据库中,所以默认的Autocomplete功能无法搜索到它们,和地理定位无关。
2. 方案一:本地自定义自动补全(快速实现)
- 把所有自定义终端的名称、坐标存入本地列表,创建一个数据类封装这些信息
- 替换默认的AutocompleteSupportFragment,用
AutoCompleteTextView实现本地搜索:- 监听输入关键词,在本地列表中匹配结果
- 选中结果后直接跳转到对应标记位置,逻辑和现有代码一致
示例代码片段:
// 自定义终端数据类 public class CustomTerminal { private String name; private LatLng latLng; public CustomTerminal(String name, LatLng latLng) { this.name = name; this.latLng = latLng; } // getter方法 public String getName() { return name; } public LatLng getLatLng() { return latLng; } } // 初始化本地终端列表 List<CustomTerminal> terminalList = new ArrayList<>(); terminalList.add(new CustomTerminal("Marisol Terminal", MarisolTerminal)); terminalList.add(new CustomTerminal("Villa Terminal", VillaTerminal)); terminalList.add(new CustomTerminal("Checkpoint Terminal", CheckpointTerminal)); // 给自定义搜索框设置适配器 AutoCompleteTextView searchView = findViewById(R.id.custom_search_view); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, terminalList.stream().map(CustomTerminal::getName).toArray(String[]::new)); searchView.setAdapter(adapter); // 监听选择事件 searchView.setOnItemClickListener((parent, view, position, id) -> { CustomTerminal selected = terminalList.get(position); CameraPosition cameraPosition = new CameraPosition.Builder() .target(selected.getLatLng()) .zoom(15) .build(); mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); });
3. 方案二:提交地点到Google Places(长期方案)
如果这些终端是真实存在的实体地点,可以通过Google Places API的Place Add功能将它们提交到Google数据库,审核通过后就能被默认Autocomplete搜索到。此流程需遵循Google审核规则,且有一定处理延迟。
4. 方案三:混合搜索(兼顾官方地点与自定义地点)
- 同时调用Google Places Autocomplete接口和本地列表匹配
- 合并两边的搜索结果,去重后展示给用户
- 该方案实现相对复杂,但能提供最完整的搜索体验
内容的提问来源于stack exchange,提问作者Chichi
相关产品推荐
相关产品推荐

