您的位置:首页 > 其它

通过地址获得经纬度(百度Geocoding API)

2014-08-12 20:29 549 查看
1.什么是Geocoding?


Geocoding API 是一类简单的HTTP接口,用于提供从地址到经纬度坐标或者从经纬度坐标到地址的转换服务,用户可以使用C# 、C++、Java等开发语言发送HTTP请求且接收JSON、XML的返回数据。

package addressToGeo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

public class GetLatAndLngByBaidu {

private static int NumOfThread = 100;
static String fileSourcePath="F://Essex//AddressToGeoAll//source//input.txt";
static String fileDestPath="F://Essex//AddressToGeoAll//source//output.txt";

public static void main(String args[]) throws InterruptedException, IOException{
long startTime=System.currentTimeMillis();
threadStart(startTime);
}
public static void threadStart(long startTime) throws InterruptedException, IOException
{
System.out.println("start...");
String cnAdd ="";
int cnLen = 0;
cnAdd = readAdd(fileSourcePath);
String[] AddStr = cnAdd.split(",");
cnLen = AddStr.length;
System.out.println("get "+cnLen+" chinese address successful...");
System.out.println("to get longitude and latitude ...");
int MatLen = cnLen/NumOfThread;
String [][] sp=new String[NumOfThread][MatLen];
MyThread [] mt=new MyThread[NumOfThread];
for(int j=0;j<NumOfThread;j++)
{
sp[j]=new String[MatLen];
for(int i=0;i<MatLen;i++)
{
sp[j][i]=AddStr[i+j*MatLen];
}
mt[j]=new MyThread("线程"+j,sp[j]);
mt[j].start();
}

for(int j=0;j<NumOfThread;j++)
{
mt[j].join();
}
String[] result;
FileWriter letter = new FileWriter(fileDestPath);

for(int j=0;j<NumOfThread;j++)
{
result = mt[j].getGpsAdd();
writeFile(result,letter,MatLen);
}

//处理结尾由于不能被100整数引起多余的几个数据
if(NumOfThread*MatLen <= cnLen-1){
String[] LastMat = new String[cnLen-NumOfThread*MatLen];
for(int i =0;i<cnLen-NumOfThread*MatLen;i++){
LastMat[i] = AddStr[NumOfThread*MatLen+i];
}
MyThread LastThread = new MyThread("线程last",LastMat);
LastThread.start();
LastThread.join();
result = LastThread.getGpsAdd();
writeFile(result, letter, cnLen-NumOfThread*MatLen);
}
letter.close();
getExcuteTime(startTime);
}

//1,申请ak(即获取密钥),若无百度账号则首先需要注册百度账号。
public static final String AK= "NXlvvuBhTW1Bd7PaatQvyceV";
public static String readAdd(String sourceFilePath){
try{
String encoding="Unicode";
File fileSource=new File(sourceFilePath);//获得输入句柄
FileInputStream fis=new FileInputStream(fileSource);//创建字节流输入对象
InputStreamReader inStream=new InputStreamReader(fis,encoding);//将字节流转换为字符流,编码格式为Unicode,创建字节流输入对象
BufferedReader input=new BufferedReader(inStream);

String cnAddress=null;
cnAddress=input.readLine();
StringBuilder sb = new StringBuilder("");
while(cnAddress!=null){
sb.append(cnAddress.trim());
sb.append(",");
cnAddress=input.readLine();
}
String sbStr = sb.toString();
//     System.out.println(sb.toString()); //输出所有地址
input.close();
return sbStr;
} catch (IOException e){
e.printStackTrace();
}
return null;
}
public static void getExcuteTime(long startTime)
{
long endTime=System.currentTimeMillis();
long time=endTime-startTime;
System.out.println("take time: "+time/1000+" second");
System.out.println("about "+time/1000/60+" minute");
}
public static void writeFile(String[] result, FileWriter writer, int MatLen) throws IOException{
for(int i = 0;i<MatLen;i++){
writer.write(result[i]);;
}
}
}


View Code

7.不足


1,网速不好的情况下,开100个线程会出现 java.net.SocketTimeoutException: connect timed out错误。

虽然使用了uc.setConnectTimeout(10000); uc.setReadTimeout(10000); 但是没有起到效果。(求改进)

2,所有的数据都会放在内存上,当数据量到达千万级别时候,会内存不足。(求改进)

8.引用

http://developer.baidu.com/map/webservice-geocoding.htm http://www.cnblogs.com/gzggyy/archive/2013/06/21/3148610.html http://lavasoft.blog.51cto.com/62575/99150 http://www.360doc.com/content/13/0422/09/3776353_280044198.shtml
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: