百度地图经纬度和地址互转(Java代码)

时间:2022-11-09 21:46:21

这是基于springmvc+mybatis 的一个controller。如果不是这个框架,可以把方法实体抽到自己写的一个类中,然后再测试

package com.uwitec.controller.system.tools;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.uwitec.controller.base.BaseController;
import com.uwitec.util.AppUtil;
import com.uwitec.util.Const;
import com.uwitec.util.MapDistance;
import com.uwitec.util.PageData;
import com.uwitec.util.PathUtil;
import com.uwitec.util.TwoDimensionCode;

@Controller
@RequestMapping(value="/tool")
public class ToolController extends BaseController {

/**

* 输入地址返回经纬度坐标 
     * key lng(经度),lat(纬度) 
     */  
@RequestMapping(value="/getXY")
    public  void getGeocoderLatitude(String address){  
        BufferedReader in = null;  
        try {  
            address = URLEncoder.encode(address, "UTF-8");  
            URL tirc = new URL("http://api.map.baidu.com/geocoder?address="+ address +"&output=json&key="+"7d9fbeb43e975cd1e9477a7e5d5e192a");  
            in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));  
            String res;  
            StringBuilder sb = new StringBuilder("");  
            while((res = in.readLine())!=null){  
                sb.append(res.trim());  
            }  
            String str = sb.toString();  
            if(StringUtils.isNotEmpty(str)){  
                int lngStart = str.indexOf("lng\":");  
                int lngEnd = str.indexOf(",\"lat");  
                int latEnd = str.indexOf("},\"precise");  
                if(lngStart > 0 && lngEnd > 0 && latEnd > 0){  
                    String lng = str.substring(lngStart+5, lngEnd);  
                    String lat = str.substring(lngEnd+7, latEnd);  
                }  
            }  
        }catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            try {  
                in.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    } 
    
    /** 
     * 输入经纬度返回地址
     * key lng(经度),lat(纬度) 
     */ 
@RequestMapping(value="/getAdr")
    public void getposition(String latitude,String longitude) throws MalformedURLException{
    BufferedReader in = null;
    URL tirc = new URL("http://api.map.baidu.com/geocoder?location="+ latitude+","+longitude+"&output=json&key="+"E4805d16520de693a3fe707cdc962045");  
         try {
in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));
String res;  
        StringBuilder sb = new StringBuilder("");  
        while((res = in.readLine())!=null){  
            sb.append(res.trim());  
        }  
        String str = sb.toString();
        //System.out.println(str);
        ObjectMapper mapper = new ObjectMapper();
           if(StringUtils.isNotEmpty(str)){  
             JsonNode jsonNode = mapper.readTree(str);
             jsonNode.findValue("status").toString();
             JsonNode resultNode = jsonNode.findValue("result");
             JsonNode locationNode = resultNode.findValue("formatted_address");
             System.out.println(locationNode);
           }  
           
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}

二:SQL语句计算经纬度距离:2017-04-07

SELECT id, ( 6371* acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25  ORDER BY id LIMIT 20

SELECT
(
6371 * acos(
cos(radians(29.490295)) * cos(radians(29.615467)) * cos(
radians(106.581515) - radians(106.486654)
) + sin(radians(29.490295)) * sin(radians(29.615467))
)
)*1000 AS distance

delimiter //

drop function if exists FUN_JW_DIST;

CREATE FUNCTION `FUN_JW_DIST`(lng1 double(15,9), lat1 double(15, 9), lng2 double(15,9), lat2 double(15,9)) RETURNS int(11)
BEGIN
DECLARE dist int;

SET dist = round((
6371 * acos(
cos(radians(lat1)) * cos(radians(lat2)) * cos(
radians(lng1) - radians(lng2)
) + sin(radians(lat1)) * sin(radians(lat2))
)
)*1000);

RETURN (dist);
END;

SELECT FUN_JW_DIST(106.486654 , 29.490295 , 106.581515 , 29.615467);