簡體   English   中英

如何使用點擊地點的緯度和經度獲取 Flutter 中地點的自動完成位置?

[英]How to get autocomplete location of places in Flutter with Latitude and Longitude of tapped place?

在我的應用程序中,我希望用戶輸入一個地點(並且 flutter_google_places 必須顯示地點建議)。 然后我想得到他點擊的那個地方的緯度和經度。

問題:我已經看到了這個答案: 如何創建一個簡單的谷歌地圖地址搜索,並在顫振中自動完成並獲取緯度和經度?

這與我的問題類似,但對我不起作用!

我的代碼:

Completer<GoogleMapController> _controller = Completer();
final api_key=API_KEY;
GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: API_KEY);
LocationData user_location=LoginPage.LD;








@override
Widget build(BuildContext context) {

   CameraPosition cuurent_location_of_user = CameraPosition(

   target: LatLng(user_location.latitude,user_location.longitude),
   zoom: 16,
 );


return Scaffold(
  body: Stack(
    children: <Widget>[
      Container(
      child:GoogleMap(
        mapType: MapType.hybrid,
        initialCameraPosition: cuurent_location_of_user,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      )
      ),
      Container(
          alignment: Alignment.center,
          child: RaisedButton(
            onPressed: () async {

               Prediction p =  await PlacesAutocomplete.show(
                  context: context, apiKey: api_key);
              displayPrediction(p);

            },


          )
      ),
    ],
  ),

);
}
Future<Null> displayPrediction(Prediction p) async {
  if (p != null) {
    PlacesDetailsResponse detail =
    await _places.getDetailsByPlaceId(p.placeId);

  var placeId = p.placeId;
  double lat = detail.result.geometry.location.lat;
  double lng = detail.result.geometry.location.lng;

  var address = await Geocoder.local.findAddressesFromQuery(p.description);

  print(lat);
  print(lng);
}

}

如果我嘗試: print(p.toString()); 它返回 null 我無法在我的代碼中找到問題 我還在 Google Cloud Service 中啟用了 Places API 並且我還使用了最新的 API_KEY

謝謝你

我想我得到了答案。 我忘記在我的 google 雲平台中創建一個結算帳戶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM