Google Place Picker API 查询应用范围内的地点

swiftGoogle

1个回答

写回答

zk779942182

2025-06-14 01:20

+ 关注

Google
Google

使用Google Place Picker API可以方便地查询应用范围内的地点。这个API提供了一个简单的界面,让用户可以在地图上选择一个地点,并获取该地点的详细信息。无论是开发地图导航应用还是提供地点搜索功能的应用,Google Place Picker API都是一个强大的工具。

使用Google Place Picker API查询地点

首先,我们需要在Google开发者控制台创建一个项目并启用Google Place Picker API。然后,我们需要获取一个API密钥,以便我们的应用可以访问该API。一旦我们获得了API密钥,我们就可以在我们的应用中使用Google Place Picker API了。

下面是一个简单的案例代码,演示了如何使用Google Place Picker API查询应用范围内的地点:

Java

import Android.content.Intent;

import Android.os.Bundle;

import Android.support.v7.app.AppCompatActivity;

import Android.view.View;

import Android.widget.Button;

import Android.widget.TextView;

import com.Google.Android.gms.common.api.GoogleApiClient;

import com.Google.Android.gms.location.places.Place;

import com.Google.Android.gms.location.places.ui.PlacePicker;

public class MAInActivity extends AppCompatActivity {

private static final int PLACE_PICKER_REQUEST = 1;

private TextView selectedPlaceTextView;

private Button pickPlaceButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setcontentView(R.layout.activity_mAIn);

selectedPlaceTextView = findViewById(R.id.selected_place_textview);

pickPlaceButton = findViewById(R.id.pick_place_button);

pickPlaceButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

try {

startActivityForResult(builder.build(MAInActivity.this), PLACE_PICKER_REQUEST);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PLACE_PICKER_REQUEST) {

if (resultCode == RESULT_OK) {

Place place = PlacePicker.getPlace(this, data);

String placeName = place.getName().toString();

String placeAddress = place.getAddress().toString();

selectedPlaceTextView.setText(placeName + ", " + placeAddress);

}

}

}

}

添加Google Place Picker界面

在上面的代码中,我们创建了一个按钮和一个文本视图。当用户点击按钮时,我们使用PlacePicker.IntentBuilder创建一个PlacePicker Intent,并通过startActivityForResult方法启动PlacePicker界面。当用户选择一个地点并返回到MAInActivity时,我们可以在onActivityResult方法中获取所选地点的详细信息,并将其显示在文本视图中。

这个案例代码只是一个简单的演示,您可以根据自己的需求进行定制。无论是显示地点详细信息还是将地点添加到地图中,Google Place Picker API都提供了相应的方法和接口。

Google Place Picker API是一个功能强大的工具,可以帮助我们在应用范围内查询地点。通过使用Google Place Picker API,我们可以轻松地实现地点搜索和地图导航功能,为用户提供更好的体验。无论是开发旅游应用还是提供本地服务的应用,Google Place Picker API都是一个值得使用的工具。

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号