dart Flutter geolocator插件Geolocator().placemarkFromAddress()和Geolocator().placemarkFromCoordinates()不起作用

oxiaedzo  于 2023-04-27  发布在  Flutter
关注(0)|答案(5)|浏览(201)

我试图搜索一个地址,然后试图通过地理定位器获得该位置的位置,然后试图将相机位置设置为这个位置,但它不起作用。
我发现Geolocator的两个方法有一些例外。Geolodator的所有其他方法对我来说都很好:

1. Geolocator().placemarkFromAddress()显示:

PlatformException(ERROR_GEOCODING_ADDRESS,服务不可用,null)

2. Geolocator().placemarkFromCoordinates()显示:

PlatformException(ERROR_GEOCODING_COORDINATES,服务不可用,null)

这里我正在尝试的代码:

try {
  List<Placemark> placemark = await Geolocator().placemarkFromAddress(searchAddress);
  Placemark newPlace = placemark[0];
  controller.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
     target: LatLng(newPlace.position.latitude,newPlace.position.longitude),
     zoom: 15.0,
  )));
}catch(e){
   print(e);
}
rpppsulh

rpppsulh1#

placemarkFromCoordinates已从插件geolocator移动到geocoding,只需在pubspec.yaml中添加插件geocoding和用户placemarkFromCoordinates

mzillmmw

mzillmmw2#

我从here中找到了这个问题的答案。
我只是重新加载我的手机/设备,它对我来说工作正常。
Tap here for more

bxjv4tth

bxjv4tth3#

现在移动到https://pub.dev/packages/geocoding

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geocoding/geocoding.dart';

class MapScreen02 extends StatefulWidget {
  @override
  _MapScreen02State createState() => _MapScreen02State();
}

class _MapScreen02State extends State<MapScreen02> {
  GoogleMapController mapController;
  String searchAdd;
  // List<Location> locations = await locationFromAddress("Gronausestraat 710, Enschede");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          GoogleMap(
            onMapCreated: onMapCreated,
            initialCameraPosition:
                CameraPosition(target: LatLng(40.7128, -74.0060), zoom: 10.0),
          ),
          Positioned(
            top: 30,
            right: 15,
            left: 15,
            child: Container(
              height: 50,
              width: double.infinity,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.white,
              ),
              child: TextField(
                decoration: InputDecoration(
                  hintText: 'Enter Address',
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(
                    left: 15,
                    top: 15,
                  ),
                  suffixIcon: IconButton(
                    icon: Icon(Icons.search),
                    onPressed: searchnavigate,
                    iconSize: 30,
                  ),
                ),
                onChanged: (val) {
                  setState(() {
                    searchAdd = val;
                  });
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

  searchnavigate() {
    locationFromAddress(searchAdd).then((result) {
      mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
        target: LatLng(result[0].latitude, result[0].longitude),
        zoom: 10,
      )));
    });
  }

  void onMapCreated(controller) {
    setState(() {
      mapController = controller;
    });
  }
}
e5nszbig

e5nszbig5#

您必须添加geoCoding插件并使用它如下:

List<Placemark> placemark = await placemarkFromCoordinates(currentLocation.latitude, currentLocation.longitude);

相关问题