def distance(source, destination, distance_in=None): """ This function use to get distance between two address. Args: source (string/list): Source address string/ lat, lng as list. destination (string/list): Destination address string/ lat, lng as list. distance_in(string): Type of distance. Example: meter, kelometer, miles. Returns: Float: Distance The return value. """ # Initialize return value. return_value = 0 try: origin = (source['longitude'], source['latitude']) destination = (destination['longitude'], destination['latitude']) # Call mapbox client to get direction services. response = Directions(MAPBOX_TOKEN["mapbox_token"]).directions( [origin, destination]) driving_routes = response.geojson() distances = driving_routes['features'][0]['properties']["distance"] if not distance_in: return_value = distances * 0.00062137 elif distance_in == "kelometer": return_value = round(distances / 1000, 2) except BaseException: return_value = 0 return return_value
def geoMessage(self, bot, update): bot.send_message(chat_id=update.message.chat_id, text="What is your origin?") origin = update.message.text # time = time.time(20) bot.send_message(chat_id=update.message.chat_id, text="What is your destination?") destination = update.message.text geocoder = mapbox.Geocoder( access_token= 'sk.eyJ1IjoiZGlhbmthbG9sNCIsImEiOiJjazhzdjE4c3QwMnlwM2Rud2EwZzg1b29iIn0.OqtyNqmiJI5q6UbWQC6oCQ' ) response = geocoder.forward('Paris, France') with open('text.json', 'w', encoding='UTF-8') as f: json.dump(response.json(), f) from mapbox import Directions resp = Directions('mapbox.driving').directions([origin, destination]) driving_routes = resp.geojson() first_route = driving_routes['features'][0] bot.send_message(chat_id=update.message.chat_id, pic=resp)
import mapbox import json geocoder = mapbox.Geocoder( access_token= 'sk.eyJ1IjoiZGlhbmthbG9sNCIsImEiOiJjazhzdjE4c3QwMnlwM2Rud2EwZzg1b29iIn0.OqtyNqmiJI5q6UbWQC6oCQ' ) response = geocoder.forward('Paris, France') with open('text.json', 'w', encoding='UTF-8') as f: json.dump(response.json(), f) from mapbox import Directions resp = Directions('mapbox.driving').directions([origin, destination]) driving_routes = resp.geojson() first_route = driving_routes['features'][0]