[英]How to implement place picker in android?
我正在尝试制作一个简单的应用程序,以在Google地图上显示所选地点的信息。 从android文档中,我发现可以通过使用Place Picker
来完成。 我正在遵循Google提供的本教程。
在本文档中,据说我必须使用以下代码来启动place picker
。
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
并且还提供了以下方法来检索用户选择的地点的详细信息。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
}
但是我的问题是我应该将此代码放在应用程序的什么位置? 在我的MapsActivity
或其他类文件中?
我下面的代码显示地图。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(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));
}
}
我应该在上述类中进行哪些更改以调用place picker
。 如果第一个代码段应放在上述类中,那么如何实现呢? 我尝试了很多次来找到这种类型的示例,但是找不到能解决我的问题的示例。特别是我已经尝试了本教程。 但这会带来很多错误。
我是android的初学者。 提前致谢!
第二个链接(truiton.com)中的教程对我来说效果很好。 您需要输入此代码
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
在一个块中,该块执行特定的操作,如教程和类中的函数。 如果它具有某些用户操作选项,则可以在MapsActivity类本身中进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.