Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Get geocode address from coordinates

I've implemented a reliable way of getting user lat,long coordinates in an app I'm creating, but can't seem to turn these coordinates into a placemark.

I'd also like to figure out what I need to use to print this data.

This is what I'm trying to get this data with:

List<Placemark> place = [];

void _getPlace() async {
  List<Placemark> newPlace = await _geolocator.placemarkFromCoordinates(_position.latitude, _position.longitude);
  print(newPlace);
  setState(() {
    place = newPlace;
  });
}

Thanks

Edit:

After CopsOnRoad's response, I'm getting this error:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: 

NoSuchMethodError: The method 'placemarkFromCoordinates' was called on null.
Receiver: null
Tried calling: placemarkFromCoordinates(51.4998, -0.129)
#0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:50:5)
#1      _NearbyPageState._getPlace 
package:tr/pages/nearby.dart:128
<asynchronous suspension>
#2      _NearbyPageState._controlCard.<anonymous closure> 
package:tr/pages/nearby.dart:629
#3      GestureRecognizer.invokeCallback 
package:flutter/…/gestures/recognizer.dart:182
#4      TapGestureRecognizer._checkUp 
package:flutter/…/gestures/tap.dart:365
#5      TapGestureRecognizer.acceptGesture 
package:flutter/…/gestures/tap.dart:312
#6      GestureArenaManager.sweep 
package:flutter/…/gestures/arena.dart:156
#7      _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent 
package:flutter/…/gestures/binding.dart:222
#8      _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (p<…>
like image 301
Jake Avatar asked Sep 16 '19 01:09

Jake


1 Answers

Update: Use geocoding

import 'package:geocoding/geocoding.dart';

List<Placemark> placemarks = await placemarkFromCoordinates(52.2165157, 6.9437819);

Old solution:

You are already there, extra stuff that you need is:

String _address = ""; // create this variable

void _getPlace() async {
  List<Placemark> newPlace = await _geolocator.placemarkFromCoordinates(_position.latitude, _position.longitude);

  // this is all you need
  Placemark placeMark  = newPlace[0]; 
  String name = placeMark.name;
  String subLocality = placeMark.subLocality;
  String locality = placeMark.locality;
  String administrativeArea = placeMark.administrativeArea;
  String postalCode = placeMark.postalCode;
  String country = placeMark.country;
  String address = "${name}, ${subLocality}, ${locality}, ${administrativeArea} ${postalCode}, ${country}";
  
  print(address);

  setState(() {
    _address = address; // update _address
  });
}
like image 142
CopsOnRoad Avatar answered Sep 18 '22 15:09

CopsOnRoad