Flutter geolocator plugin Geolocator().placemarkFromAddress() and Geolocator().placemarkFromCoordinates() do not work

5,490

Solution 1

placemarkFromCoordinates has been moved from plugin 'geolocator' to 'geocoding', just add plugin 'geocoding' in pubspec.yaml and user placemarkFromCoordinates

Solution 2

Its now moved to 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;
    });
  }
}

Solution 3

I find out the solution for this question from here.

I Just reload my phone/device it works fine for me.

Tap here for more

Share:
5,490
Md Mahmudul Islam
Author by

Md Mahmudul Islam

Think simple do innovative. I am from: Bangladesh Folow me: Portfolio LinkedIn Github Facebook

Updated on December 15, 2022

Comments

  • Md Mahmudul Islam
    Md Mahmudul Islam over 1 year

    I am trying to search an address and then trying to get position of that location by Geolocator and then trying to set camera position to this position but it does not work.

    I find out some exceptions for two methods of Geolocator. All others methods of Geolodator work fine for me:

    1. Geolocator().placemarkFromAddress() shows:

    PlatformException(ERROR_GEOCODING_ADDRESS, Service not Available, null)

    2. Geolocator().placemarkFromCoordinates() shows:

    PlatformException(ERROR_GEOCODING_COORDINATES, Service not Available, null)


    Here Code that I am trying:

    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);
    }
    
    • Nick Wassermann
      Nick Wassermann about 4 years
      Are you connected to the internet on your testing device ?
    • Md Mahmudul Islam
      Md Mahmudul Islam about 4 years
      yes. I am running this app in real device and internet connection was available. All others methods of geolocator works fine.
    • Nick Wassermann
      Nick Wassermann about 4 years
      Maybe some problem with your ISP or country you are working from because in my demo project this is just working fine (Austria)