Java通过google map api实现地址解析的方法

 

地址解析就是将地址(如:广东省广州市)转换为地理坐标(如经度:113.26442,纬度:23.129074)的过程。google map api提供两种方法实现地址解析。

  第一种,是通过使用 GClientGeocoder 对象来实现,大家可以参考google map api的相关文档,以下是摘自google的相关代码

var map = new GMap2(document.getElementById("map_canvas"));
  var geocoder = new GClientGeocoder();
  function showAddress(address) {
  geocoder.getLatLng(
  address,
  function(point) {
  if (!point) {
  alert("无法解析:" + address);
  } else {
  map.setCenter(point, 13);
  var marker = new GMarker(point);
  map.addOverlay(marker);
  marker.openInfoWindowHtml(address);
  }
  }
  );
  }var map = new GMap2(document.getElementById("map_canvas"));
  var geocoder = new GClientGeocoder();
  function showAddress(address) {
  geocoder.getLatLng(
  address,
  function(point) {
  if (!point) {
  alert("无法解析:" + address);
  } else {
  map.setCenter(point, 13);
  var marker = new GMarker(point);
  map.addOverlay(marker);
  marker.openInfoWindowHtml(address);
  }
  }
  );
  }

 


 var map = new GMap2(document.getElementById("map_canvas"));
  var geocoder = new GClientGeocoder();
  function showAddress(address) {
  geocoder.getLatLng(
  address,
  function(point) {
  if (!point) {
  alert("无法解析:" + address);
  } else {
  map.setCenter(point, 13);
  var marker = new GMarker(point);
  map.addOverlay(marker);
  marker.openInfoWindowHtml(address);
  }
  }
  );
  }

 

  第二种方法,就是通过HTTP请求直接访问。调用参数等相关说明请参考http://code.google.com/intl/zh-CN/apis/maps/documentation/services.html

 

/**
  * 利用googlemap api 通过 HTTP 进行地址解析
  * @param address 地址
  * @return HTTP状态代码,精确度(请参见精确度常数),纬度,经度
  */
  private String getLatlng(String address){
  String ret = "";
  if(address != null && !address.equals("")){
  try {
  address = URLEncoder.encode(address,"UTF-8");//进行这一步是为了避免乱码
  } catch (UnsupportedEncodingException e1) {
  logger.error("转码失败", e1);
  }
  String[] arr = new String[4];
  arr[0] = address;
  arr[1] = OUTPUT;
  arr[2] = SENSOR;
  arr[3] = KEY;
  String url = MessageFormat.format("http://maps.google.com/maps/geo?q={0}&output={1}&sensor={2}&key={3}",arr);
  URL urlmy = null;
  try {
  urlmy = new URL(url);
  HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
  con.setFollowRedirects (true );
  con.setInstanceFollowRedirects(false );
  con.connect();
  BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
  String s = "";
  StringBuffer sb = new StringBuffer("");
  while ((s = br.readLine()) != null ) {
  sb.append(s+"/r/n");
  }
  ret = ""+sb;
  } catch (MalformedURLException e) {
  logger.error("通过http方式获取地址信息失败", e);
  } catch (IOException e) {
  logger.error("文件读取失败", e);
  }
  }
  return ret;
  }

  而通过java实现的方法如下

 


 /**
  * 利用googlemap api 通过 HTTP 进行地址解析
  * @param address 地址
  * @return HTTP状态代码,精确度(请参见精确度常数),纬度,经度
  */
  private String getLatlng(String address){
  String ret = "";
  if(address != null && !address.equals("")){
  try {
  address = URLEncoder.encode(address,"UTF-8");//进行这一步是为了避免乱码
  } catch (UnsupportedEncodingException e1) {
  logger.error("转码失败", e1);
  }
  String[] arr = new String[4];
  arr[0] = address;
  arr[1] = OUTPUT;
  arr[2] = SENSOR;
  arr[3] = KEY;
  String url = MessageFormat.format("http://maps.google.com/maps/geo?q={0}&output={1}&sensor={2}&key={3}",arr);
  URL urlmy = null;
  try {
  urlmy = new URL(url);
  HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
  con.setFollowRedirects (true );
  con.setInstanceFollowRedirects(false );
  con.connect();
  BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
  String s = "";
  StringBuffer sb = new StringBuffer("");
  while ((s = br.readLine()) != null ) {
  sb.append(s+"/r/n");
  }
  ret = ""+sb;
  } catch (MalformedURLException e) {
  logger.error("通过http方式获取地址信息失败", e);
  } catch (IOException e) {
  logger.error("文件读取失败", e);
  }
  }
  return ret;
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
任何 Google 地图 API 应用程序中的基础元素都是“地图”本身。 大量应用js,访问GoogleAPI。 在此之前较为麻烦的事申请Google 的key,相比都知道,Google 在中国市场的地位,因此Google 的key较难申请, .Google Maps API 申请方式变更在Google Maps API 的部份,官方网站目前是建议使用新版本(v3)的API 进行开发,若您仍希望继续使用v2 的版本进行开发,您可能需要进行部份设定的修改,底下是设定的流程:   1.使用Google帐号登入APIs Console (https://code.google.com/apis/console),并将Google Maps API v2的选项打开,如下图所示: Google Maps API v2设置   2.依据您的需求产生对应的Google Maps API Key ,若您是要继续采用v2 版本的方式使用Google Maps API ,请选择「Create new Browser Key」,之后将产生的Key 依据过去的方式填入对应的位置即可 注意V2版本对应的选项   需要注意的是:   1.依目前观察,已经在使用中的Google Maps API Key 是可以继续使用,并不会因为产生Key 的方式改变而有所变更。   2.新的方式会统计并限制使用量(目前是限制每天25,000 次查询),若超过用量则需给付相关费用才可继续使用。 最近发现好多google的域名不能访问了!包括Android开发SDK都不能更新了!解决方法: 修改hosts文件 添加: 74.125.71.99 code.google.com 74.125.71.99 encrypted.google.com 74.125.71.99 suggestqueries.google.com 74.125.71.99 mail.google.com 74.125.71.99 groups.google.com 74.125.71.99 groups.google.com.hk 74.125.71.99 docs.google.com 74.125.71.99 docs0.google.com 74.125.71.99 docs1.google.com 74.125.71.99 spreadsheets.google.com 74.125.71.99 spreadsheets0.google.com 74.125.71.99 webcache.googleusercontent.com 74.125.71.99 sites.google.com 74.125.71.99 talkgadget.google.com 74.125.71.99 clients1.google.com 74.125.71.99 clients2.google.com 74.125.71.99 clients3.google.com 74.125.71.99 clients4.google.com 整个开发有些事情较做只可意会,不可言传,当然纯属的代码并不是最大的效益,需要文档的可以找我,工程部署不了的可以联系我。 email:lcyangmi@163.com MSN:yummy@chinaclick.com.cn

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值