preface

Through Baidu map, Tencent map, etc., can be specific address and longitude and latitude data in the middle of the conversion.Copy the code

The preparatory work

Ak to baidu map open platform, application, under normal circumstances, can only be free to use the 6000 times a day, each request service https://lbsyun.baidu.com/, best time intervals, such as 0.2 seconds Or to tencent location services, application key, https://lbs.qq.com/Copy the code

1/ Use Tencent map code

import pandas as pd
import numpy as np
import myutil
import requests
import datetime
from tqdm import * 
import time
from loguru import logger
import xlrd
import csv
import json

pd.set_option('display.max_rows'.None)
pd.set_option('display.max_columns'.None)
pd.set_option('max_colwidth'.500)
pd.set_option('display.width'.10000)

# applied by Tencent Map developer Key
tengxun_map_developer_key = xxxxxxx 


Enter the address to get the latitude and longitude
def address_to_coordinate(address) :
    url = "http://apis.map.qq.com/ws/geocoder/v1/?address={}&key={}".format(address, tengxun_map_developer_key)  # Address resolve, address transfer coordinates
    response = requests.get(url)
    answer = response.json()
  
    if answer['status'] = =0:  If the status code is 0, the access is successful
        lng = answer['result'] ['location'] ["lng"]  # longitude
        lat = answer['result'] ['location'] ["lat"]  # latitude
    else:
        lng = np.nan
        lat = np.nan
        print("No specific latitude and longitude obtained!!")
   
    return lng,lat

Enter the latitude and longitude to get the address
# The input lat_LNG parameter is a string
# Latitude is connected in front, middle, like "40.066479,116.309303"

def coordinate_to_address(lat_lng) :  # Notice the input format: latitude, longitude
    url = "https://apis.map.qq.com/ws/geocoder/v1/?location={}&get_poi={}&key={}".format(lat_lng, 1,tengxun_map_developer_key)  # Coordinate transfer address
    response = requests.get(url)
    answer = response.json()
    #print(answer)
    
    if answer['status'] = =0:
        address = answer['result'] ['address']
    else:
        address = 'error'
        print(address)
        
    return address
Copy the code

2/ Use Baidu map code

import pandas as pd
import numpy as np
import myutil
import requests
import datetime
from tqdm import * 
import time
from loguru import logger
import xlrd
import csv
import json

pd.set_option('display.max_rows'.None)
pd.set_option('display.max_columns'.None)
pd.set_option('max_colwidth'.500)
pd.set_option('display.width'.10000)

# applied by Tencent Map developer Key
baidu_map_developer_key = xxxxxxx 

def get_location_from_baidu_map(addr) :
    url = "http://api.map.baidu.com/geocoding/v3/?" # Baidu Map API interface
    
    para = {"address": addr, # Pass in the address parameter
            "output": "json"."ak": baidu_map_developer_key  # Baidu Map open platform application AK
           }
           
    req = requests.get(url,para)
    req = req.json()

    if req["status"] = =0:
        lng = req["result"] ["location"] ["lng"]  # longitude
        lat = req["result"] ["location"] ["lat"]  # latitude
    else:
        lng = np.nan
        lat = np.nan

    return lng,lat
Copy the code