eoinzy
eoinzy

Reputation: 2242

python - urls.py regex help with coordinates

I tried searching but could not find an answer for this.

I am trying to write a function that takes in coordinates, ie latitude and longitude. For example, 53.345633,-6.267014.

These will then be fed into the Google Maps API as the users current location, and directions to some nearby places (I already have these locations stored somewhere) and the directions will hopefully be returned.

So, I pretty much have all the Maps work done, but I can't test it because, frustratingly enough, I simply cannot figure out the regex for inside urls.py.

Can anyone help me with this? I'm hoping its simple enough for you guys. I tried it earlier but failed miserably! It's so frustrating coz I'm so close to finishing this part too!!

Thanks for the help!

PS Is the format for coordinates advisable, with the comma? Perhaps 53.345633+-6.267014 would be better (then I can just use my_coords = coords.replace("+", ", ") or something)??

Upvotes: 2

Views: 1656

Answers (2)

ocobacho
ocobacho

Reputation: 551

If you want the dot to be optional.

(?P<lon>-?\d+.?\d+)/(?P<lat>-?\d+.?\d+) 

Upvotes: 0

OneOfOne
OneOfOne

Reputation: 99224

I'm not sure I get it but you can try something like :

def str2cords(scords):
    return [float(c) for c in scords.split(',')] #or maybe just scords.split(',') since float might mess up the exact cords?

or regex :

'/(-?\d+\.\d+),(-?\d+\.\d+)/'

Upvotes: 5

Related Questions