调用高德API,通过输入的地址,如省份、市、区获取经纬度 ,通过输入的经纬度,获取区域详情

一、pom

<?xml version="1.0" encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0        http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.java.gaode.GaoDeDemo</groupId>
    <artifactId>GaoDeDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.google.collections</groupId>
            <artifactId>google-collections</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.9</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.31</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.19.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.28</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
    </dependencies>
</project>

二、HttpClientUtils工具类

package com.java.gao;
/**
 * @ClassName: HttpClientUtils
 * @Description:
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2019/10/22 0022 下午 8:15
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2019/10/22 0022        ***      v1.0.0         创建
 */
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NoHttpResponseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.nio.charset.CodingErrorAction;
import java.util.List;
import java.util.Map;


/**
 * HttpClient工具类
 */
public class HttpClientUtils {
    /**
     * 连接池最大连接数
     */
    private static final int MAX_TOTAL_CONNECTIONS = 4000;

    /**
     * 设置每个路由上的默认连接个数
     */
    private static final int DEFAULT_MAX_PER_ROUTE = 200;

    /**
     * 请求的请求超时时间 单位:毫秒
     */
    private static final int REQUEST_CONNECTION_TIMEOUT = 8 * 1000;

    /**
     * 请求的等待数据超时时间 单位:毫秒
     */
    private static final int REQUEST_SOCKET_TIMEOUT = 8 * 1000;

    /**
     * 请求的连接超时时间 单位:毫秒
     */
    private static final int REQUEST_CONNECTION_REQUEST_TIMEOUT = 5 * 1000;

    /**
     * 连接闲置多久后需要重新检测 单位:毫秒
     */
    private static final int VALIDATE_AFTER_IN_ACTIVITY = 2 * 1000;

    /**
     * 关闭Socket时,要么发送完所有数据,要么等待多少秒后,就关闭连接,此时socket.close()是阻塞的 单位秒
     */
    private static final int SOCKET_CONFIG_SO_LINGER = 60;

    /**
     * 接收数据的等待超时时间,即读超时时间,单位ms
     */
    private static final int SOCKET_CONFIG_SO_TIMEOUT = 5 * 1000;
    /**
     * 重试次数
     */
    private static int RETRY_COUNT = 5;
    /**
     * 声明为 static volatile,会迫使线程每次读取时作为一个全局变量读取
     */
    private static volatile CloseableHttpClient httpClient = null;


    /**
     * @param uri
     * @return String
     * @description get请求方式
     * @author: long.he01
     */
    public static String doGet(String uri) {

        String responseBody;
        HttpGet httpGet = new HttpGet(uri);
        try {
            httpGet.setConfig(getRequestConfig());
            responseBody = executeRequest(httpGet);
        } catch (IOException e) {
            throw new RuntimeException("httpclient doGet方法异常 ", e);
        } finally {
            httpGet.releaseConnection();
        }

        return responseBody;
    }

    /**
     * @param uri
     * @param params
     * @return string
     * @description 带map参数get请求, 此方法会将map参数拼接到连接地址上。
     */
    public static String doGet(String uri, Map<String, String> params) {

        return doGet(getGetUrlFromParams(uri, params));

    }

    /**
     * @param uri
     * @param params
     * @return String
     * @description 根据map参数拼接完整的url地址
     */
    private static String getGetUrlFromParams(String uri, Map<String, String> params) {


        List<BasicNameValuePair> resultList = FluentIterable.from(params.entrySet()).transform(
                new Function<Map.Entry<String, String>, BasicNameValuePair>() {
                    @Override
                    public BasicNameValuePair apply(Map.Entry<String, String> innerEntry) {

                        return new BasicNameValuePair(innerEntry.getKey(), innerEntry.getValue());
                    }

                }).toList();

        String paramSectionOfUrl = URLEncodedUtils.format(resultList, Consts.UTF_8);
        StringBuffer resultUrl = new StringBuffer(uri);

        if (StringUtils.isEmpty(uri)) {
            return uri;
        } else {
            if (!StringUtils.isEmpty(paramSectionOfUrl)) {
                if (uri.endsWith("?")) {
                    resultUrl.append(paramSectionOfUrl);
                } else {
                    resultUrl.append("?").append(paramSectionOfUrl);
                }
            }
            return resultUrl.toString();
        }


    }


    /**
     * @param uri
     * @param params
     * @return String
     * @description 带map参数的post请求方法
     */
    public static String doPost(String uri, Map<String, String> params) {

        String responseBody;
        HttpPost httpPost = new HttpPost(uri);
        try {
            List<NameValuePair> nvps = Lists.newArrayList();
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                nvps.add(new BasicNameValuePair(key, value));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
            httpPost.setConfig(getRequestConfig());
            responseBody = executeRequest(httpPost);

        } catch (Exception e) {
            throw new RuntimeException("httpclient doPost方法异常 ", e);
        } finally {
            httpPost.releaseConnection();
        }

        return responseBody;

    }

    /**
     * @param uri
     * @param param
     * @param contentType 根据具体请求情况指定,比如json可以是 ContentType.APPLICATION_JSON
     * @return String
     * @description 带单string参数执行post方法
     */
    public static String doPost(String uri, String param, ContentType contentType) {

        String responseBody;
        HttpPost httpPost = new HttpPost(uri);
        try {
            StringEntity reqEntity = new StringEntity(param, contentType);
            httpPost.setEntity(reqEntity);
            httpPost.setConfig(getRequestConfig());
            responseBody = executeRequest(httpPost);

        } catch (IOException e) {
            throw new RuntimeException("httpclient doPost方法异常 ", e);
        } finally {
            httpPost.releaseConnection();
        }
        return responseBody;
    }

    /**
     * @return RequestConfig
     * @description: 获得请求配置信息
     */
    private static RequestConfig getRequestConfig() {


        RequestConfig defaultRequestConfig = RequestConfig.custom()
                //.setCookieSpec(CookieSpecs.DEFAULT)
                .setExpectContinueEnabled(true)
                //.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
                //.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
                .build();

        return RequestConfig.copy(defaultRequestConfig)
                .setSocketTimeout(REQUEST_CONNECTION_TIMEOUT)
                .setConnectTimeout(REQUEST_SOCKET_TIMEOUT)
                .setConnectionRequestTimeout(REQUEST_CONNECTION_REQUEST_TIMEOUT)
                .build();

    }


    /**
     * @param method
     * @return String
     * @throws IOException
     * @description 通用执行请求方法
     */
    private static String executeRequest(HttpUriRequest method) throws IOException {

        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

            @Override
            public String handleResponse(final HttpResponse response) throws IOException {

                int status = response.getStatusLine().getStatusCode();
                String result;
                if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
                    HttpEntity entity = response.getEntity();
                    result = entity != null ? EntityUtils.toString(entity) : null;
                    EntityUtils.consume(entity);
                    return result;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }

        };
        String result = getHttpClientInstance().execute(method, responseHandler);

        return result;
    }


    /**
     * @return CloseableHttpClient
     * @description 单例获取httpclient实例
     */
    private static CloseableHttpClient getHttpClientInstance() {

        if (httpClient == null) {
            synchronized (CloseableHttpClient.class) {
                if (httpClient == null) {
                    httpClient = HttpClients.custom().setConnectionManager(initConfig()).setRetryHandler(getRetryHandler()).build();
                }
            }
        }
        return httpClient;

    }

    /**
     * @return HttpRequestRetryHandler
     * @description :获取重试handler
     */
    private static HttpRequestRetryHandler getRetryHandler() {

        // 请求重试处理
        return new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException exception,
                                        int executionCount, HttpContext context) {
                if (executionCount >= RETRY_COUNT) {
                    // 假设已经重试了5次,就放弃
                    return false;
                }
                if (exception instanceof NoHttpResponseException) {
                    // 假设server丢掉了连接。那么就重试
                    return true;
                }
                if (exception instanceof SSLHandshakeException) {
                    // 不要重试SSL握手异常
                    return false;
                }
                if (exception instanceof InterruptedIOException) {
                    // 超时
                    return false;
                }
                if (exception instanceof UnknownHostException) {
                    // 目标server不可达
                    return false;
                }
                if (exception instanceof ConnectTimeoutException) {  
                                  
				// 连接被拒绝
				return false;        
        }                

				if (exception instanceof SSLException) {                    
				// SSL握手异常
				return false;                
					}                
				HttpRequest request = HttpClientContext.


				adapt(context).getRequest();                
				// 假设请求是幂等的,就再次尝试
				return !(request instanceof HttpEntityEnclosingRequest);         
				   }       
				    }; 
				       }    
		/**
		* @return PoolingHttpClientConnectionManager
		* @description 初始化连接池等配置信息
		*/
		private static PoolingHttpClientConnectionManager initConfig() {      
		
				Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>
		create().register("http", PlainConnectionSocketFactory.INSTANCE)                .register("https", new SSLConnectionSocketFactory(SSLContexts.createSystemDefault()))                .build();       
		PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);        

/**
* 以下参数设置含义分别为:
* 1 是否立即发送数据,设置为true会关闭Socket缓冲,默认为false
* 2 是否可以在一个进程关闭Socket后,即使它还没有释放端口,其它进程还可以立即重用端口
* 3 接收数据的等待超时时间,单位ms
* 4 关闭Socket时,要么发送完所有数据,要么等待多少秒后,就关闭连接,此时socket.close()是阻塞的
* 5 开启监视TCP连接是否有效
* 其中setTcpNoDelay(true)设置是否启用Nagle算法,设置true后禁用Nagle算法,默认为false(即默认启用Nagle算法)。
* Nagle算法试图通过减少分片的数量来节省带宽。当应用程序希望降低网络延迟并提高性能时,
* 它们可以关闭Nagle算法,这样数据将会更早地发 送,但是增加了网络消耗。 单位为:毫秒
*/

        SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(
					true).setSoReuseAddress(
					true).setSoTimeout(
					SOCKET_CONFIG_SO_TIMEOUT)                
					//.setSoLinger(SOCKET_CONFIG_SO_LINGER)
					//.setSoKeepAlive(true)
					.build();
					connManager.setDefaultSocketConfig(socketConfig);        connManager.setValidateAfterInactivity(VALIDATE_AFTER_IN_ACTIVITY);        ConnectionConfig connectionConfig = ConnectionConfig.custom()                .setMalformedInputAction(CodingErrorAction.
		IGNORE).setUnmappableInputAction(CodingErrorAction.
		IGNORE).setCharset(Consts.UTF_8).build();        connManager.setDefaultConnectionConfig(connectionConfig);        connManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE); 
		       connManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);        
return connManager;    
}}

三、GaoDeMapUtils工具类

package com.java.gao;
import com.sun.deploy.net.URLEncoder;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
 * @ClassName: GaoDeMapUtils
 * @Description:
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2019/10/22 0022 下午 8:05
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2019/10/22 0022        ***     v1.0.0         创建
 */
public class GaoDeMapUtils {

    /**
     * 高德地图请求秘钥
     */
    private static final String KEY = "高德官网申请";
    /**
     * 返回值类型
     */
    private static final String OUTPUT = "JSON";
    /**
     * 根据地名获取高德经纬度
     */
    private static final String GET_LNG_LAT_URL = "http://restapi.amap.com/v3/geocode/geo";
    /**
     * 根据高德经纬度获取地名
     */
    private static final String GET_ADDRESS_URL = "http://restapi.amap.com/v3/geocode/regeo";

    /**
     * 根据高德经纬度获取地址信息
     *
     * @param gdLon 高德地图经度
     * @param gdLat 高德地图纬度
     * @return
     */
    public static String getAddressByLonLat(double gdLon, double gdLat) {

        String location = gdLon + "," + gdLat;
        Map<String, String> params = new HashMap<>();
        params.put("location", location);

        try {
            // 拼装url,output返回值类型,GET_ADDRESS_URL根据高德经纬度获取地名
            String url = jointUrl(params, OUTPUT, KEY, GET_ADDRESS_URL);
            // 调用高德SDK
            return HttpClientUtils.doPost(url, params);
            // 解析Json字符串,获取城市名称
            // JSONObject jsonObject = JSON.parseObject(jsonResult);
            // String regeocode = jsonObject.getString("regeocode");
            // JSONObject regeocodeObj = JSON.parseObject(regeocode);
            // String address = regeocodeObj.getString("formatted_address");
            // 组装结果
            // result.put(location, address);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据地址信息获取高德经纬度
     *
     * @param address 地址信息
     * @return
     */
    public static String getLonLarByAddress(String address) {
        Map<String, String> params = new HashMap<>();
        params.put("address", address);

        // Map<String, String> result = new HashMap<>();
        try {
            // 拼装url
            String url = jointUrl(params, OUTPUT, KEY, GET_LNG_LAT_URL);
            // 调用高德地图SDK
            return HttpClientUtils.doPost(url, params);

            // 解析JSON字符串,取到高德经纬度
            // JSONObject jsonObject = JSON.parseObject(jsonResult);
            // JSONArray geocodes = jsonObject.getJSONArray("geocodes");
            // String geocode = JSON.toJSONString(geocodes.get(0));
            // JSONObject geocodeObj = JSON.parseObject(geocode);
            // String lonAndLat = geocodeObj.getString("location");
            // 组装结果
            // result.put(address, lonAndLat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 拼接请求字符串
     *
     * @param params
     * @param output
     * @param key
     * @param url
     * @return
     * @throws IOException
     */
    private static String jointUrl(Map<String, String> params, String output, String key, String url) throws IOException {
        StringBuilder baseUrl = new StringBuilder();
        baseUrl.append(url);

        int index = 0;
        Set<Map.Entry<String, String>> entrys = params.entrySet();
        for (Map.Entry<String, String> param : entrys) {
            // 判断是否是第一个参数
            if (index == 0) {
                baseUrl.append("?");
            } else {
                baseUrl.append("&");
            }
            baseUrl.append(param.getKey()).append("=").append(URLEncoder.encode(param.getValue(), "utf-8"));
            index++;
        }
        baseUrl.append("&output=").append(output).append("&key=").append(key);

        return baseUrl.toString();
    }


}

四、测试

package com.java.gao;
/**gao包里的三个方法是根据经纬度获取区域或者根据区域获取经纬度
 * 本方法两个功能:通过输入的地址,如省份、市、区获取经纬度
 *   通过输入的经纬度,获取地址值
 *  总共三个类:Controller、GaoDeMapUtils、HttpClientUtils工具类
 * @ClassName: Controller
 * @Description:
 * @Version: v1.0.0
 * @Author: Fu Hao
 * @Date: 2019/10/22 0022 下午 8:23
 * Modification History:
 * Date           Author      Version     Description
 * -------------------------------------------------------------
 * 2019/10/22 0022        ****     v1.0.0         创建
 */


public class Controller {
    public static void main(String[] args) {
        System.out.println(GaoDeMapUtils.getLonLarByAddress("中国"));

        System.out.println("---------------------------------------");

        System.out.println(GaoDeMapUtils.getAddressByLonLat(118,29));

    }
}

直接导入数据不调用第三方接口实现参考博客:https://blog.csdn.net/qq_45614471/article/details/118994501

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: JavaScript 高德地图 API 可以通过经纬度获取地理位置信息。该 API 使用了逆地理编码技术,可以将地理位置经纬度坐标转换成详细的地址描述信息。您可以使用 JavaScript 调用 API,传递经度和纬度参数,即可获取对应的定位信息。 在使用高德地图 API 的时候,需要先通过 JavaScript 加载地图库,然后创建一个地图容器,指定容器的宽度和高度,设置地图的中心点和缩放级别。然后,在 JavaScript 代码中调用逆地理编码方法,传递经度和纬度参数,该方法会返回一个地址描述对象,包含国家、省份、城县、街道等详细描述信息。 除了通过 JavaScript 调用高德地图 API 进行地理位置获取,还可以使用其他的开发工具和平台进行位置定位,如百度地图、谷歌地图等。这些地图 API 都提供了类似的功能,可以通过经纬度获取地址信息,并进行地理位置展示、导航等操作。 ### 回答2: JavaScript 高德地图 API 是一个基于 JavaScript 的地理信息可视化库,它可以帮助我们在 web 上创建交互式地图应用程序,使用 API 获取和展示地理位置信息,也可以通过 API 提供的方法来对地图进行交互。 我们可以使用经纬度获取地理位置信息,一般来说,我们需要使用地图 API 提供的 geocoder 对象的 search 方法,将需要查询的经纬度作为参数传递过去。geocoder 对象是地理编码与逆地理编码服务的核心类,它把地理信息转换为经纬度坐标和反之。在 JavaScript 高德地图 API 中,我们可以通过以下代码来实现经纬度与地理位置之间的转换: ``` var geocoder, map = new AMap.Map("container"); //初始化地理编码器 geocoder = new AMap.Geocoder({ city: "010" //城,默认:“全国” }); //调用搜索方法,获取地理位置信息 geocoder.search([116.480983, 39.989628], function(status, result) { //根据状态判断是否查询成功 if (status === 'complete' && result.info === 'OK') { //地理位置信息 var address = result.regeocode.formattedAddress; console.log(address); } else { console.log("查询失败!"); } }); ``` 在这个例子中,我们创建了一个 geocoder 对象,并传递了城参数。然后,我们调用了 geocoder 的 search 方法,并向其中传递了一个包含需要查询的经纬度的数组。当查询成功时,我们可以通过 result 对象获取到地理位置信息,其中包含了详细地址、城省份等信息。从中,我们可以获取到我们需要的详细地址信息,来帮助我们完成相应的功能。 总之,JavaScript 高德地图 API 提供了非常方便的地理位置信息获取方式。我们可以通过 geocoder 对象的 search 方法来获取经纬度对应的地理位置信息,方便我们开发与地理位置相关的应用程序。 ### 回答3: JS高德地图API提供了通过经纬度获取地理位置的功能。下面是具体的步骤: 1.在使用该功能前,需要在高德地图开发者平台注册并申请相应的API Key。 2.在HTML页面中引入高德地图API的JavaScript文件,同时创建一个显示地图的DOM元素。 3.利用JavaScript获取用户的经纬度信息。 4.将获取到的经纬度信息作为参数,调用API中的逆地理编码模块,获取经纬度所处的地理位置信息。 5.将获取到的位置信息通过JavaScript动态渲染到页面上。 具体代码实现如下: 先在HTML页面中引入高德地图API的JavaScript文件: ``` <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.7&key=YOUR_API_KEY"></script> ``` 其中,YOUR_API_KEY需要替换成你所申请的API Key。 创建一个用于显示地图的DOM元素: ``` <div id="map" style="width: 100%; height: 500px;"></div> ``` 获取用户的经纬度信息: ``` navigator.geolocation.getCurrentPosition(function(position) { var longitude = position.coords.longitude; var latitude = position.coords.latitude; // TODO: 调用API获取地理位置信息 }); ``` 将经纬度信息作为参数,调用API的逆地理编码模块: ``` var geocoder = new AMap.Geocoder({ city: "", // 城,默认:“全国” }); geocoder.getAddress([longitude, latitude], function(status, result) { if (status === 'complete' && result.regeocode) { var address = result.regeocode.formattedAddress; // TODO: 将位置信息渲染到页面上 } }); ``` 其中,geocoder.getAddress()方法会返回一个Result对象,里面包含了位置信息,例如formattedAddress、addressComponent等。 最后,将位置信息通过JavaScript动态渲染到页面上即可。 总之,JS高德地图API提供了通过经纬度获取地理位置信息的功能,使用起来相对简单方便。在使用该功能时,需要注意申请API Key并确保用户已经授权获取其地理位置信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值