java从输入中获取经纬度_java从百度后台接口获取经纬度
首先,我需要从文本文档里获取地址信息
package com.tech.lgyj;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import com.tech.lgyj.domain.Central;
public class ReadMapTxt {
public ArrayListreadFileByLines(String fileName) {
BufferedReader reader = null;
ArrayListarrayList = new ArrayList<>();
try{
System.out.println("以行为单位读取txt");
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "gbk"));
String tempString = null;
int line = 1;
//一次读一行,直到读入null为结束
while((tempString = reader.readLine()) != null)
{
String temp2 = tempString.replace(" ", "");
System.out.println("line " + line + ":" + temp2);
Central central = new Central();
central.setAddress(temp2);
arrayList.add(temp2);
line++;
}
reader.close();
}catch(IOException e)
{
e.printStackTrace();
}finally {
if(reader != null)
{
try
{
reader.close();
}catch(IOException e1)
{
e1.printStackTrace();
}
}
}
return arrayList;
}
//public static void readFileByLines(String fileName)
//{
//BufferedReader reader = null;
//
//try{
//System.out.println("以行为单位读取txt");
//reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "gbk"));
//String tempString = null;
//int line = 1;
一次读一行,直到读入null为结束
//while((tempString = reader.readLine()) != null)
//{
//String temp2 = tempString.replace(" ", "");
//System.out.println("line " + line + ":" + temp2);
//
//Central central = new Central();
//central.setAddress(temp2);
//
//line++;
//
//}
//
//reader.close();
//}catch(IOException e)
//{
//e.printStackTrace();
//}finally {
//if(reader != null)
//{
//try
//{
//reader.close();
//}catch(IOException e1)
//{
//e1.printStackTrace();
//}
//}
//}
//}
}
然后创建与百度接口的链接然后获取经纬度并放在map里
package com.tech.lgyj;
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 java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
//import net.sf.json.JSONObject;
public class MapBaidu {
public MapgetLngAndLat(String address){
Mapmap=new HashMap();
String url = "http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak=C5dae37ba0a97216555b8bf2b76bacfd";
String json = loadJSON(url);
JSONObject obj = JSONObject.parseObject(json);
if(obj.get("status").toString().equals("0")){
double lng=obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
double lat=obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
map.put("lng", lng);
map.put("lat", lat);
System.out.println("经度:"+lng+"---纬度:"+lat);
}else{
System.out.println(address + "未找到相匹配的经纬度!");
}
return map;
}
public String loadJSON (String url) {
StringBuilder json = new StringBuilder();
try {
URL oracle = new URL(url);
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine = null;
while ( (inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return json.toString();
}
}
然后调用方法存入表里
@Override
@Transactional(rollbackFor={RuntimeException.class,Exception.class})
public Msg addLocation() {
//获取本地文本地址里面的内容
String file_name = "C:/Users/Administrator/Desktop/地址111.txt";
ReadMapTxt mapTxt = new ReadMapTxt();
ArrayListarrayList = mapTxt.readFileByLines(file_name);
for(int i=0;imap = mapBaidu.getLngAndLat(address);
Central central = new Central();
central.setAddress(address);
if(map.get("lng") != null)
{
central.setLng(map.get("lng").toString());
central.setLat(map.get("lat").toString());
}
int ret = centralMapper.addLocation(central);
if (ret < 0) {
return factory.failMsg("增加失败");
}
}
return factory.successMsg("增加成功");
// TODO Auto-generated method stub
//try {
//for(int i=0;i < centrals.size();i++)
//{
//int ret = centralMapper.addLocation(centrals.get(i));
//if (ret < 0) {
//return factory.failMsg("增加失败");
//}
//}
//return factory.successMsg("增加成功");
//} catch (Exception e) {
TODO: handle exception
//throw new ManagedException(e.getMessage());
//}
}
java从输入中获取经纬度_java从百度后台接口获取经纬度相关推荐
- java提取word中的图片_Java怎么获取Word批注的文字和图片
Java怎么获取Word批注的文字和图片 发布时间:2020-06-23 11:07:08 来源:亿速云 阅读:119 作者:Leah 这篇文章将为大家详细讲解有关Java获取Word批注的文字和图片 ...
- java清空inputstream中的流_Java 流处理
数据流 1.I/O流概述 大部分程序都需要输入/输出处理,比如从键盘读取数据.向屏幕中输出数据.从文件中读或者向文件中写数据.在一个网络连接上进行读写操作等.在Java中,把这些不同类型的输入.输出源 ...
- java 从excel中读取数据_java如何从excel中读出数据
package myexceltest; import jxl.*; import java.io.*; import jxl.write.*; /** * java读取Excel表格,拷贝.更新Ex ...
- java反射 获取属性_Java反射学习-3 - 反射获取属性,方法,构造器
1 packagecn.tx.reflect;2 3 importjava.lang.reflect.Constructor;4 importjava.lang.reflect.Field;5 imp ...
- java线程future_Java中多线程对运行结果怎么利用future获取
Java中多线程对运行结果怎么利用future获取 发布时间:2020-12-07 17:11:08 来源:亿速云 阅读:124 作者:Leah Java中多线程对运行结果怎么利用future获取?针 ...
- Java从输入中读取一个数组
最近在编程中遇到了如下问题:如何从输入中读取一个数组,一直调试出问题,后来查阅了一些资料终于解决了. 这主要是关于scanner扫描器中的nextLine()方法以及spilt()方法的应用,代码如下 ...
- java删除数组中指定元素_java学习中如何删除数组中的指定元素
java的api中,并没有提供删除数组中元素的方法.虽然数组是一个对象,不过并没有提供add()remove()或查找元素的方法.这就是为什么类似ArrayList和HashSet受欢迎的原因. 不过 ...
- java 去掉字符串中的换行_java删除字符串中的空格、回车、换行符、制表符程序...
本程序来详细的介绍关于java删除字符串中的空格.回车.换行符.制表符程序实例,有需要学习的朋友可参考参考. java换行符 那是因为默认的情况下,界面上的换行时用的"n",这是L ...
- (简单详细)React Native获取手机当前经纬度,(通过高德地图接口)获取当前地理位置
要获取手机的当前地理位置,首先要获得经纬度,然后通过逆地理编码获得位置信息 地理编码/逆地理编码 API 是通过 HTTP/HTTPS 协议访问远程服务的接口,提供结构化地址与经纬度之间的相互转化的能 ...
最新文章
- win10+计算机安全配置,做好个人电脑安全隐私设置Windows10系统磁盘数据加密操作...
- Jeecg-boot 使用心得建议
- java lang保_android – Gson中的RuntimeException解析JSON:无法调用受保护的java.lang.ClassLoader()而没有args...
- 罗永浩或将找陈冠希做代言人
- 马云的妻子张瑛有多优秀?
- matlab2c使用c++实现matlab函数系列教程-max函数
- 某IDC服务商机房宕机致银行业务中断 银监会发布风险提示
- 网易云音乐实时数仓2.0进阶之路
- html5三国策略,HTML5新游《乱弹三国》今日首曝
- Bootstrap3基础 img-rounded 图片的四个角改成圆角
- php获取判断图片大小,php 获取图片尺寸的方法
- shell特殊符号cut命令,sort、wc、uniq命令,tee、tr、split命令
- 群晖同步数据到天翼网盘
- Redis是什么?有什么用?
- 标准分幅下的图幅号转换成经纬度坐标【原理+源代码】
- 现代企业管理笔记——管理概论
- 可解释机器学习-shap value的使用
- 国际化资源文件命名规范
- 代码随想录算法训练营第15天,102.二叉树的层序遍历、226.翻转二叉树、101.对称二叉树
- word中迅速将表格一分为二 拆分表格快捷键