java 实现百度地图的地址解析及在数据库的读取存储过程

package action;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class LocationUtil {

private static final String ak = "XG8Ed4Vi6BN67wrmFLc7x63oYOayXIR1";
        
        /**
        * 返回输入地址的经纬度坐标 key lng(经度),lat(纬度)
        */
        public static Map<String, String> getLatitude(String address) {
                try {
                        // 将地址转换成utf-8的16进制
                        address = URLEncoder.encode(address, "UTF-8");
                        // 如果有代理,要设置代理,没代理可注释
                        // System.setProperty("http.proxyHost","192.168.172.23");
                        // System.setProperty("http.proxyPort","3209");
                                                          
                        URL resjson = new URL("http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address="
                                        + address + "&city=广州市&output=json&ak=" + ak);
                      //  System.out.println(resjson);
                        BufferedReader in = new BufferedReader(new InputStreamReader(resjson.openStream()));
                        String res;
                        StringBuilder sb = new StringBuilder("");
                        while ((res = in.readLine()) != null) {
                                sb.append(res.trim());
                        }
                        in.close();
                        String str = sb.toString();
                        //System.out.println("return json:" + str);
                        if(str!=null&&!str.equals("")){
                                Map<String, String> map = null;
                                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 + 6, lngEnd);
                                        String lat = str.substring(lngEnd + 7, latEnd);
                                        map = new HashMap<String, String>();
                                        map.put("lng", lng);
                                        map.put("lat", lat);
                                        return map;
                                }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null;
        }
           
        public static void main(String args[]) {
                
                Map<String, String> map = LocationUtil.getLatitude("越秀区人民中路1号");
                if (null != map) {
                        System.out.println(map.get("lng"));
                        System.out.println(map.get("lat"));
                }
        }
}

package action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class JDBC {

public static void main(String[] args) {

Connection connection = null;
                Statement statement = null;
                try {
                        //1、加载数据库驱动
                        Class.forName("oracle.jdbc.OracleDriver");
                        //2、创建数据库连接
                        String url = "jdbc:oracle:thin:@localhost:1520:orcl";
                        connection = DriverManager.getConnection(url, "scot", "tiger");
                        
                        //3、进行数据库操作
                        statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);//Oracle数据库需要设置才能进行updata
                        ResultSet rs = statement.executeQuery("select djxh,dz,gps from t_location");//sql语句中出现多余的标点符号 ;,  会报错无效字符
                        LocationUtil locationUtil=new LocationUtil();
                        while(rs.next()){
                           // System.out.println(rs.toString());
                            String a=rs.getNString("djxh");
                            String bString=rs.getNString("dz");
                           Map  cString=locationUtil.getLatitude(bString);
                            String dString =cString .get("lng").toString()+","+cString.get("lat").toString();/得到经纬度
                            //System.out.println(dString);
                            rs.updateString("gps",dString);//存入数据库
                            rs.updateRow();
                            System.out.println(rs.getNString("dz")+" : "+rs.getNString("gps"));
                            
                     
                   
                        }
                        rs.close();
                        
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        //4、关闭连接
                        try {
                                if(statement != null){
                                        statement.close();
                                }
                                if(connection != null){
                                        connection.close();
                                }
                        } catch (SQLException e) {
                                e.printStackTrace();
                        }
                }
        }

}

时间: 2024-11-13 16:11:21

java 实现百度地图的地址解析及在数据库的读取存储过程的相关文章

百度地图之地址解析和浏览器定位以及添加可拖拽标注

这里的百度地图demo包括了常见的:地址解析.浏览器定位.IP定位.可拖拽标注 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> <script type="tex

百度地图的使用之获取数据库表中信息的坐标点显示在地图上

//通用封装好的js: var map = new BMap.Map("container"); //建树Map实例 var point = new BMap.Point(103.976032, 33.845509); // 建树点坐标 map.centerAndZoom(point, 6); // 初始化地图,设置中心点坐标和地图级别. //地图事件设置函数: map.enableDragging(); //启用地图拖拽事件,默认启用(可不写) map.enableScrollWhe

java调用百度地图非原创

1 <%@ page contentType="text/html; charset=gb2312"%> 2 <!DOCTYPE html> 3 <html> 4 <head> 5 <title>百度地图javascript</title> 6 <script type="text/javascript"> 7 function initialize() { 8 var mp = n

java调用百度地图API根据地理位置获取经纬度

public Map<String, BigDecimal> getLatAndLngByAddress(String addr){ String address = ""; String lat = ""; String lng = ""; try { address = java.net.URLEncoder.encode(addr,"UTF-8"); } catch (UnsupportedEncodingE

百度地图 逆地址解析

地址/逆地址 解析: http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding 示例代码: http://bbs.it-home.org/thread-63503-1-1.html sn签名: http://lbsyun.baidu.com/index.php?title=lbscloud/api/appendix

在Fragment中实现百度地图,定位到当前位置(基于SDKv2.1.0)

使用最新版本的百度地图需要注意的几个地方: 1.libs文件夹下要有android-support-v4.jar.baidumapapi_v2_1_0.jar.locSDK_3.1.jar三个jar包和armeabi文件夹. 2.布局文件要写成<com.baidu.mapapi.map.MapView />,旧版本是写作<com.baidu.mapapi.MapView />的. 3.AndroidMainfest这个配置文件要把该添加的东西都加上.比如各种权限.比如屏幕支持.比如

Vue项目引用百度地图并实现搜索定位等功能

Tip:本篇文章为案例分析,技术点较多,所以篇幅较长,认真阅览的你一定会学到很多知识. 前言:百度地图开放平台 给开发者们提供了丰富的地图功能与服务,使我们的项目中可以轻松地实现地图定位.地址搜索.路线导航等功能.本文给大家介绍如何在vue项目中引用百度地图,并设计实现简单的地图定位.地址搜索功能. 一.效果图及功能点 先来看一下效果图 效果图看不够? 点此 试试在线操作!(初次进入加载较慢,请耐心等待) 功能点: 挂载百度地图 封装逆地址解析函数(根据坐标点获取详细地址) 设置图像标注并绑定拖

百度地图API实现批量地址解析

1.前言 写这篇文章的原因是最近做一个GIS项目在网上爬取了一些数据,无奈只有地址的文字信息没有坐标信息,如何把信息显现在地图上呢?很纠结啊,查看了一下百度地图API惊奇的发现百度提供了地址解析的API,然后查看了他的Demo后豁然开朗,所以动手将自己的文字信息数据进行解析坐标信息.下面开始讲解. 2.方案 (1)自己数据库中的数据 (2)百度地图API Demo <!DOCTYPE html> <html> <head> <meta http-equiv=&qu

百度地图定位 java.lang.NullPointerException

整了四五天显示的地图终于不是北京了,心酸泪一把T~T. java.lang.NullPointerException 是空指针异常,问候了度娘,又看了几遍自己的代码都没觉得有异常, 突发奇想的改了改代码竟然成了..... (下面是改完后的代码) 1 public class Local_MainActivity extends Activity { 2 private MapView mapView; 3 private BaiduMap baiduMap; 4 private Location