3

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);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Md Mahmudul Islam
  • 2,514
  • 4
  • 18
  • 33

5 Answers5

9

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

Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40
Ghazanfar Abbas
  • 106
  • 1
  • 3
1

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

Md Mahmudul Islam
  • 2,514
  • 4
  • 18
  • 33
1

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;
    });
  }
}
0

Use GeoCoding package instead.

swapnil kumar
  • 101
  • 1
  • 1
  • 5
0

You have to add the geoCoding plugin and use it like this:

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