Places SDK สำหรับ Android (เดิม) รองรับสถานที่ปัจจุบัน (เดิม) หากคุณคุ้นเคยกับสถานที่ปัจจุบัน (เดิม) การค้นหาใกล้เคียง (ใหม่) จะทำการเปลี่ยนแปลงต่อไปนี้
ใช้รูปแบบการกำหนดราคาใหม่ ดูข้อมูลราคาสำหรับ API ทั้งหมดได้ที่ Places SDK สำหรับ Android (ใหม่)
คุณต้องเริ่มต้นแอปโดยเรียกใช้เมธอด
Places.initializeWithNewPlacesApiEnabled()ดูข้อมูลเพิ่มเติมเกี่ยวกับการเลือกบริการ Places API ได้ที่ ตั้งค่าโปรเจ็กต์ Google Cloudต้องมีการมาสก์ฟิลด์ คุณต้องระบุช่องที่ต้องการให้แสดงผล ในการตอบกลับ ไม่มีรายการฟิลด์ที่แสดงผลเริ่มต้น หากคุณละเว้น รายการนี้ เมธอดจะแสดงข้อผิดพลาด
การค้นหาใกล้เคียง (ใหม่) ไม่รองรับ
PlaceLikelihoodการค้นหาใกล้เคียง (ใหม่) ช่วยให้คุณใช้ลำดับผลการค้นหาเพื่อระบุตำแหน่งที่มีความเป็นไปได้มากที่สุดได้
ตัวอย่างการค้นหาในบริเวณใกล้เคียง (ใหม่)
ดูข้อมูลเพิ่มเติมและตัวอย่างวิธีใช้การค้นหาใกล้เคียง (ใหม่) ได้ในเอกสารประกอบการค้นหาใกล้เคียง (ใหม่)
ใช้การค้นหาใกล้เคียง (ใหม่) เพื่อรับสถานที่ปัจจุบัน
ตัวอย่างต่อไปนี้แสดงวิธีรับสถานที่ปัจจุบันด้วยการค้นหาใกล้เคียง (ใหม่) โดยแทนที่การใช้ PlacesClient.findCurrentPlace() ด้วย PlacesClient.searchNearby()
public class MainActivity extends AppCompatActivity {
private FusedLocationProviderClient fusedLocationProviderClient;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// ...
// get permission
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
// get location and search
fusedLocationProviderClient
.getLastLocation()
.addOnSuccessListener(
this,
location -> {
if (location != null) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CircularBounds circle = CircularBounds.newInstance(latLng, 100);
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.DISPLAY_NAME);
// Define a list of types to exclude. Adjust this list to suit each application.
final List<String> excludedTypes = Arrays.asList("public_bathroom", "beach");
SearchNearbyRequest request
= SearchNearbyRequest.builder(/* location restriction = */ circle, placeFields)
.setExcludedTypes(excludedTypes)
.setRankPreference(SearchNearbyRequest.RankPreference.DISTANCE)
.build();
placesClient
.searchNearby(request)
.addOnSuccessListener(
response -> {
List<Place> places = response.getPlaces();
// do more on the results
})
.addOnFailureListener(
exception -> {
// handle failure
});
} else {
// failed to get location.
}
})
.addOnFailureListener(
e -> {
// handle error
});
} else {
ActivityCompat.requestPermissions(
this,
new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);
}
}
}