android - 迁移到 Places API,无法解析 GEO_DATA_API GeoDataApi

标签 android google-places-api google-places googleplacesautocomplete

我正在经历从废弃的 Places SDK 迁移到 Places API 的过程,如 here 所述。 ,使用兼容性库。在尝试迁移之前,一切都运行良好。我有

1) 更新了我的依赖

2) 更改我的导入语句

3) Min SDK 已经是 21

我收到两个(看似相关的)错误。 cannot find symbol variable GEO_DATA_APIcannot find symbol variable GeoDataApi
编码

googleApiClient = new GoogleApiClient.Builder(PlacesActivity.this)
            .addApi(Places.GEO_DATA_API)  //***HERE***
            .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
            .addConnectionCallbacks(this)
            .build();


private ArrayList<PlaceAutocomplete> getPredictions(CharSequence constraint) {
    if (googleApiClient !=null) {
        PendingResult<AutocompletePredictionBuffer> results = Places.GeoDataApi.getAutocompletePredictions(  // ***AND HERE***
                googleApiClient,
                constraint.toString(),
                latLngBounds,
                autocompleteFilter
        );

        // Wait for predictions, set the timeout.
        AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS);

        final Status status = autocompletePredictions.getStatus();
        if (!status.isSuccess()) {
            //auto complete fail
            autocompletePredictions.release();
            return null;
        }
        //auto complete success
        Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
        ArrayList<PlaceAutocomplete> resultList = new ArrayList<>(autocompletePredictions.getCount());
        while (iterator.hasNext()) {
            AutocompletePrediction prediction = iterator.next();
            resultList.add(new PlaceAutocomplete(prediction.getPlaceId(), prediction.getFullText(null)));
        }
        // Buffer release
        autocompletePredictions.release();
        return resultList;
    }
    return null;
}

最佳答案

需要完全重写代码。这是获取 lat、lng 和名称的工作代码(例如)

public class MainActivity extends AppCompatActivity {
    String TAG = "placeautocomplete";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize Places.
    Places.initialize(getApplicationContext(), "YOUR_API_KEY");
    // Create a new Places client instance.
    PlacesClient placesClient = Places.createClient(this);

    // Initialize the AutocompleteSupportFragment.
    AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

    // Specify the types of place data to return.
    autocompleteFragment.setPlaceFields(Arrays.asList(
         Place.Field.NAME,
         Place.Field.LAT_LNG
    ));

    // Set up a PlaceSelectionListener to handle the response.
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            String name = place.getName();
            double lat, lng;
            if (place.getLatLng() !=null){
                lat =place.getLatLng().latitude;
                lng =place.getLatLng().longitude;
            }

            //do something
        }

        @Override
        public void onError(Status status) {
            // TODO: Handle the error.
            Log.i(TAG, "An error occurred: " + status);
        }
    });
}
}

示例 xml
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <android.support.v7.widget.CardView
        android:id="@+id/idCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardCornerRadius="4dp"
        >
        <fragment
            android:id="@+id/autocomplete_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
            />
    </android.support.v7.widget.CardView>
</LinearLayout>

关于android - 迁移到 Places API,无法解析 GEO_DATA_API GeoDataApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55982638/

相关文章:

react-native - map 始终位于搜索位置标记的中心

android - 如何准备要发布的 Jetpack compose 应用程序,@Preview 可组合项呢?

java - 在列表中加载静态图像会减慢滚动速度

javascript - Google map 地点 javascript 文本搜索 : can't get radius to work

java - 如何使用 Hibernate 避免 MySQL 数据库中的重复条目?

java - Lollipop 及更低版本上的 Android Google Places API

java - 无法在 Firebase valueEventListener 中使用 Context

python - 是否可以在 Django 中制作移动应用程序?

google-maps - 如何仅在边界内获取 Places.GeoDataApi.getAutocompletePredictions ?

javascript - 用户共享的 GEOlocation 不适用于谷歌地点