Skip to content

Commit

Permalink
Add Gaode Map API.
Browse files Browse the repository at this point in the history
  • Loading branch information
manateelazycat committed Jan 8, 2024
1 parent f07917f commit d180abf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ This repository provides the EAF OpenStreetMap application for the [Emacs Applic
| `s` | save_map |
| `t` | js_toggle_distance_tip |

### Gaode Map API
If you have [Gaode Map API Key](https://lbs.amap.com), put fill key in ```~/.emacs.d/eaf/map/gaode_api_key.txt```, then eaf-map will use Gaode API instead OpenStreetMap API, API's response is much faster in China.
68 changes: 48 additions & 20 deletions buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def handle_open_map(self, filepath):
message_to_emacs("Path {} not exist, please input valid emap path.".format(filepath))

@PostGui()
def fetch_address_list(self, location):
def fetch_address_list(self, locations):
self.send_input_message("Select address to add: ", "select_address", "list",
completion_list=list(map(lambda loc: "{}#{}#{}".format(loc["display_name"], loc["lon"], loc["lat"]), location)))
completion_list=list(map(lambda loc: "{}#{}#{}".format(loc["display_name"], loc["lon"], loc["lat"]), locations)))

@PostGui()
def no_address_found(self, new_place):
Expand All @@ -170,35 +170,63 @@ def __init__(self, new_place):

self.new_place = new_place

def fetch_url(self, url):
import pycurl
from io import BytesIO
def fetch_locations(self):
gaode_api_key_path = os.path.expanduser("~/.emacs.d/eaf/map/gaode_api_key.txt")
gaode_api_key = ""
if os.path.exists(gaode_api_key_path):
with open(gaode_api_key_path) as f:
gaode_api_key = f.read().strip()

if gaode_api_key != "":
import requests
import json

url = 'https://restapi.amap.com/v3/geocode/geo'
params = { 'key': gaode_api_key, 'address': self.new_place}
res = requests.get(url, params)
content = json.loads(res.text)

if content["status"] == "1":
geocodes = content["geocodes"]
locations = list(map(lambda geocode: {
"display_name": geocode["formatted_address"],
"lon": geocode["location"].split(",")[0],
"lat": geocode["location"].split(",")[1]
}, geocodes))
return locations
else:
return []
else:
import pycurl
from io import BytesIO

buffer = BytesIO()
url = 'https://nominatim.openstreetmap.org/search.php?q={}&format=jsonv2'.format(urllib.parse.quote(self.new_place))

c = pycurl.Curl()
c.setopt(c.URL, url)
buffer = BytesIO()

c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
c = pycurl.Curl()
c.setopt(c.URL, url)

body = buffer.getvalue()
content = body.decode('utf-8')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()

try:
return json.loads(content)
except:
return []
body = buffer.getvalue()
content = body.decode('utf-8')

try:
return json.loads(content)
except:
return []

def run(self):
try:
location = self.fetch_url('https://nominatim.openstreetmap.org/search.php?q={}&format=jsonv2'.format(urllib.parse.quote(self.new_place)))
locations = self.fetch_locations()

if len(location) == 0:
if len(locations) == 0:
self.no_address_found.emit(self.new_place)
else:
self.fetch_address_finish.emit(location)
self.fetch_address_finish.emit(locations)
except:
import traceback
traceback.print_exc()
Expand Down

0 comments on commit d180abf

Please sign in to comment.