获取google地图经纬度

方法一:JS方法

<html> 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>

<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css"/>

<script type="text/javascript"src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder
=new google.maps.Geocoder();
var latlng
=new google.maps.LatLng(34.264987, 108.94426900000007);
var myOptions
= {
zoom:
12,
center: latlng,
mapTypeId:google.maps.MapTypeId.ROADMAP
}
map
=new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//初始化标注添加以下代码
marker
=new google.maps.Marker({
title:
'',
map: map,
position:latlng
});
var infowindow =new google.maps.InfoWindow({
content:
'<strong>love</strong>'
});
infowindow.open(map, marker);
}

functioncodeAddress() {
varaddress
= document.getElementById("address").value;
geocoder.geocode({
'address': address }, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[
0].geometry.location);
this.marker =newgoogle.maps.Marker({
title: address,
map: map,
position:results[
0].geometry.location
});
var infowindow =newgoogle.maps.InfoWindow({
content:
'<strong>'+ address +'</strong><br/>'+'纬度: '+ results[0].geometry.location.lat() +'<br/>经度: '+ results[0].geometry.location.lng()
});
infowindow.open(map, marker);
}
else {
alert(
"Geocode was not successful for the following reason:"+ status);
}
alert(
'纬度: '+ results[0].geometry.location.lat() +'<br/>经度: '+ results[0].geometry.location.lng())
});
}

</script>
</head>
<body onload="initialize()">
<div>
<input id="address" type="text" value="西安市">
<input type="button" value="地址解析" onclick="codeAddress()">
</div>
<div id="map_canvas" style="height:90%;top:30px"></div>
</body>
</html>


方法二:ASP.NET方法

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

//使用方法,g.Latitude=经度,g.Longtitude=纬度
//mapClass.Geo g = new mapClass.Geo("西安");
//Response.Write(g.Latitude + "<br>" +g.Longtitude);
namespace mapClass
{
  publicclassGeo
  {
  ///
  /// latitude
  ///
  privatestring _latitude = "";
  ///
  /// longtitude
  ///
  privatestring _longtitude = "";
  ///
  /// default constructor
  ///
  publicGeo()
  {
  }

  ///
  /// construct geo given latitude and longtitude
  ///
  publicGeo(string latitude, stringlongtitude)
  {
    _latitude = latitude;
    _longtitude =longtitude;
  }

  ///
  /// construct geo given name of a place
  ///
  publicGeo(string location)
  {
    stringoutput = "csv";
    stringurl = string.Format("http://maps.google.com/maps/geo?q={0}&output={1}",location, output);
    try
    {
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
      using (HttpWebResponseresponse = (HttpWebResponse)request.GetResponse())
      {
      using (StreamReadersr = newStreamReader(response.GetResponseStream()))
      {
      string[] tmpArray = sr.ReadToEnd().Split(',');
      _latitude =tmpArray[2];
      _longtitude= tmpArray[3];
      }
    }
    }
    catch(System.Net.Sockets.SocketException ex)
    {
      Console.WriteLine("网络中断");
    }
    catch(Exception ex)
    {
      //throw ex;
      Console.WriteLine("异常类型:{0}", ex.GetType());
      Console.WriteLine("异常信息:{0}", ex.Message);
      Console.WriteLine("异常来源:{0}", ex.Source);
      Console.WriteLine("异常堆栈:{0}", ex.StackTrace);
      Console.WriteLine("内部异常:{0}", ex.InnerException);
    }
  }
  ///
  /// get latitude
  ///
  publicstring Latitude
  {
    get{ return _latitude; }
    set{ _latitude = value; }
  }

  ///
  /// get longtitude
  ///
  publicstring Longtitude
  {
    get{ return _longtitude; }
    set{ _longtitude = value; }
  }
  }
}



posted @ 2012-03-24 09:30  WebApi  阅读(1940)  评论(0编辑  收藏  举报
CopyRight © 博客园 WebAPI