java Google Place Picker Android Studio

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43109350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:05:16  来源:igfitidea点击:

Google Place Picker Android Studio

javaandroidgoogle-mapsgoogle-places-api

提问by Justin Luna

Does anyone know how to implement Google Place Picker API in Android Studio? Tried tutorials, unsuccessful. I want a working AutoComplete Text Field that has places show up and when you click on the place it will display in the Google Map Fragment.

有谁知道如何在 Android Studio 中实现 Google Place Picker API?试过教程,不成功。我想要一个显示位置的工作自动完成文本字段,当您单击该位置时,它将显示在 Google 地图片段中。

回答by Jd Prajapati

Add the dependency to yourbuild.gradle:

将依赖项添加到您的build.gradle

compile "com.google.android.gms:play-services-places:10.2.0"

Add your key to your AndroidManifest.xml:

将您的密钥添加到您的AndroidManifest.xml

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/map_api_key"/>

Your Activity/Fragment needs these attributes and methods:

您的 Activity/Fragment 需要这些属性和方法:

private final static int PLACE_PICKER_REQUEST = 999;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    checkPermissionOnActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        switch (requestCode){
        case PLACE_PICKER_REQUEST:
            Place place = PlacePicker.getPlace(this, data);
            String placeName = String.format("Place: %s", place.getName());
            double latitude = place.getLatLng().latitude;
            double longitude = place.getLatLng().longitude;

        }
    }
}

finally, this is the code to open a PlacePicker

最后,这是打开一个的代码 PlacePicker

PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
     // for activty
     startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
     // for fragment         
     //startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST); 
} catch (GooglePlayServicesRepairableException e) {
     e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
     e.printStackTrace();
}

回答by Null Pointer Exception

call this method on Button clicklistner or where you want to open PlacePicker:

在 Button clicklistner 或要打开 PlacePicker 的位置调用此方法:

private final static int PLACE_PICKER_REQUEST = 111;
 private void openPlacePicker() {
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
        try {
            // for activty
            startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
            // for fragment
            //startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }

and fetch the result onActivityResult:

并在 ActivityResult 上获取结果:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PLACE_PICKER_REQUEST:
                    Place place = PlacePicker.getPlace(this, data);
                    String placeName = String.format("Place: %s", place.getName());
                    double latitude = place.getLatLng().latitude;
                    double longitude = place.getLatLng().longitude;
                    LatLng coordinate = new LatLng(latitude, longitude);
                    MarkerOptions markerOptions = new MarkerOptions();
                    markerOptions.position(coordinate);
                    markerOptions.title(placeName); //Here Total Address is address which you want to show on marker
                    mGoogleMap.clear();
                    markerOptions.icon(
                            BitmapDescriptorFactory
                                    .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

                    markerOptions.getPosition();
                    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
                    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(coordinate));
                    mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

            }
        }
    }