我正在开发一个Android项目,我想搜索附近的地方(例如医院),但是这段代码不起作用。
从我在调试模式中看到的情况来看,它跳过了这一行:placesClient.findCurrentPlace(request).addOnCompleteListener(new OnCompleteListener
当然还有这一行下面的代码。 有人知道为什么吗?您可以在下面看到完整的代码。
Places.initialize(this, getString(R.string.google_maps_key));
PlacesClient placesClient = Places.createClient(this);
List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME);
FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
placesClient.findCurrentPlace(request).addOnCompleteListener(new OnCompleteListener<FindCurrentPlaceResponse>() {
@Override
public void onComplete(@NonNull Task<FindCurrentPlaceResponse> task) {
FindCurrentPlaceResponse response = task.getResult();
for(PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()){
Place place = placeLikelihood.getPlace();
List<Place.Type> types = place.getTypes();
if(types != null && types.contains(Place.Type.HOSPITAL)){
map.addMarker(new MarkerOptions()
.position(place.getLatLng())
.title(place.getName())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));
}
}
}
});
检查事项:
首先,仔细检查 logcat,因为您可能会遇到 API 密钥设置问题或权限错误。这两者都存在问题是很常见的。
其次,使用 OnCompleteListener 设置调试断点或添加可在 logcat 中找到的日志消息。这里的目标是确保回调被调用。在提供的代码片段中,除非找到医院,否则不会执行任何明显的操作。
第三,考虑更新相机以确保视图集中在结果上。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
com.example.playground.databinding.ActivityMapsBinding binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
// Normal, you should check that the app has the required permissions.
@SuppressLint("MissingPermission")
void getCurrentPlace() {
String apiKey = BuildConfig.PLACES_API_KEY;
Places.initialize(this, apiKey);
PlacesClient placesClient = Places.createClient(this);
List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.LAT_LNG);
FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);
placesClient.findCurrentPlace(request)
.addOnCompleteListener(task -> {
FindCurrentPlaceResponse response = task.getResult();
LatLngBounds.Builder bounds = LatLngBounds.builder();
for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
Place place = placeLikelihood.getPlace();
Log.e("PlacesDemo", "Found place: " + place.getName());
List<Place.Type> types = place.getTypes();
if (types != null && types.contains(Place.Type.STORE)) {
mMap.addMarker(new MarkerOptions()
.position(Objects.requireNonNull(place.getLatLng()))
.title(place.getName())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));
} else {
mMap.addMarker(new MarkerOptions()
.position(Objects.requireNonNull(place.getLatLng()))
.title(place.getName())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
if (place.getLatLng() != null)
bounds.include(place.getLatLng());
}
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 100));
})
.addOnFailureListener(task -> {
String message = task.getMessage();
Log.e("PlacesDemo", message);
});
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
getCurrentPlace();
}
}