2

i want to pass my latitude and langitude values to my flask route but everytime i am getting this error: ValueError: Must be a coordinate pair or Point

however i have tried this and its working fine:

from flask import Flask, render_template 
from geopy.geocoders import Nominatim


app = Flask(__name__)

geolocator = Nominatim()



@app.route('/location')
def lang_and_lat():
    location = geolocator.reverse("21.0943, 81.0337")

    address = location.address

    return render_template('ip.html', address=address)


if __name__ == '__main__':
    app.run(debug=True)
from flask import Flask, render_template 
from geopy.geocoders import Nominatim


app = Flask(__name__)

geolocator = Nominatim()



@app.route('/location/<lat>/<lang>')
def lang_and_lat(lat, lang):
    location = geolocator.reverse(lat, lang)

    address = location.address

    return render_template('ip.html', address=address)


if __name__ == '__main__':
    app.run(debug=True)

vatsalay
  • 469
  • 1
  • 9
  • 21
  • 1
    in the first snippet you pass one positional argument of type `str` which represent latitude and longitude separated by comma, while in the second you pass 2 positional arguments of type `str`. – buran Sep 03 '19 at 10:48
  • i want to pass latitude and longitude values as arguments to my flask route – vatsalay Sep 03 '19 at 11:11
  • You are calling a function in an API. You can't just pass the values the way you want; you have to pass them in a way that the API function is prepared to accept them. All examples I've seen of geolocator.reverse take a string with the two values separated by a comma. The error message you are getting indicates that you should be able to put the two values together in a Point object and pass that, but I have not seen any such examples. – Basya Sep 03 '19 at 11:13
  • Then do so, but then join them in single `str` as expected by `geolocator.reverse()` – buran Sep 03 '19 at 11:13
  • I found this: https://www.programcreek.com/python/example/93521/geopy.Point, which shows how to create a geopy.Point object: origin = geopy.Point(latitude=lat, longitude=lon) – Basya Sep 03 '19 at 11:14
  • @buran thanks for the suggestion. I'll try that – vatsalay Sep 03 '19 at 11:16

1 Answers1

3

you need to do location = geolocator.reverse(f'{lat}, {lang}')

or location = geolocator.reverse(Point(lat, lang)) in second case you need to from geopy.point import Point

buran
  • 13,682
  • 10
  • 36
  • 61