获取两地之间距离(真实地址或经纬度)高德API-JAVA版

此工具类有两个功能,一是,传入两个真实地址,可以得到这两个地址间距离; 二是,传入两个经纬度,也可以得到两者相距多少,单位是米
package util;
import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.net.MalformedURLException;  
import java.net.URL;  
import java.net.URLConnection;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;  
  
/**
 * 1.这个需要客户申请个高德API上面的key,也可以自己申请(免费的,很快)
 * */
public class GetDistance {  
    public static void main(String[] args){  
        String start = "北京天安门";  
        String end = "上海市黄浦区西藏中路";  
  
        String startLonLat = getLonLat(start);  
        String endLonLat = getLonLat(end);  
  
        System.out.println("起始地:"+start+",经纬度:"+startLonLat);  
        System.out.println("终点:"+end+",经纬度:"+endLonLat);  

 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以使用以下代码进行高德经纬度距离计算和驾驶距离计算: ```php //高德地图api获取两地经纬度 $origin = "北京市朝阳区阜通东大街6号"; $destination = "北京市海淀区中关村南大街甲2号"; $origin_url = "https://restapi.amap.com/v3/geocode/geo?key=YOUR_KEY&address=".urlencode($origin); $destination_url = "https://restapi.amap.com/v3/geocode/geo?key=YOUR_KEY&address=".urlencode($destination); $origin_json = file_get_contents($origin_url); $origin_data = json_decode($origin_json,true); $origin_location = explode(",",$origin_data['geocodes'][0]['location']); $destination_json = file_get_contents($destination_url); $destination_data = json_decode($destination_json,true); $destination_location = explode(",",$destination_data['geocodes'][0]['location']); $origin_lng = $origin_location[0]; $origin_lat = $origin_location[1]; $destination_lng = $destination_location[0]; $destination_lat = $destination_location[1]; //高德经纬度距离计算 $distance_url = "https://restapi.amap.com/v3/distance?key=YOUR_KEY&origins={$origin_lng},{$origin_lat}&destination={$destination_lng},{$destination_lat}&type=1"; $distance_json = file_get_contents($distance_url); $distance_data = json_decode($distance_json,true); $distance = $distance_data['results'][0]['distance']; //高德地图api计算驾驶距离 $driving_url = "https://restapi.amap.com/v3/direction/driving?key=YOUR_KEY&origin={$origin_lng},{$origin_lat}&destination={$destination_lng},{$destination_lat}"; $driving_json = file_get_contents($driving_url); $driving_data = json_decode($driving_json,true); $duration = $driving_data['route']['paths'][0]['duration']; $distance_driving = $driving_data['route']['paths'][0]['distance']; ``` 请将上述代码中的 `YOUR_KEY` 替换为你的高德地图 API 密钥。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艺术2333

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值