首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请教Google Map API如何根据地址获得经纬度

2012-04-13 
请问Google Map API怎么根据地址获得经纬度?请问Google Map API怎么根据地址获得经纬度?根据输入的地址,获

请问Google Map API怎么根据地址获得经纬度?
请问Google Map API怎么根据地址获得经纬度?
根据输入的地址,获得该地址的经纬度?
求代码

[解决办法]
1.<html>
2.<head>
3.<meta http-equiv="Content-Type" content="text ml; charset=utf-8">
4.<title>MAPBaidu-http://www.k686.com</title>
5.<script type="text/javascript" src=" http://api.map.baidu.com/api?key=458d39374361da27e548367a735831ba&v=1.0&services=true" ></script>
6.</head>
7.<body>
8.<div style="position:absolute;width:730px;height:590px;top:50;left:0;border:1px solid gray;overflow-y:hidden;" id="container"></div>
9.<input id="text_" type="text" value="武汉"/>
10.<input type="button" value="search" onclick="searchByStationName();">
11.<a href="http://www.k686.com" target="_blank">k686绿色软件</a>
12.</body>
13.<script>
14.var map = new BMap.Map("container");
15.map.centerAndZoom(new BMap.Point(121.480,31.220), 6);
16.
17.var localSearch= new BMap.LocalSearch (map, {
18. renderOptions: {
19. pageCapacity: 8,
20. autoViewport: true,
21. selectFirstResult: false
22. }
23. });
24.
25. localSearch.enableAutoViewport();
26.function searchByStationName()
27.{
28.
29. var keyword = document.getElementById("text_").value;
30. localSearch.setSearchCompleteCallback(function(searchResult){
31. var poi = searchResult.getPoi(0);
32. alert(poi.point.lng+" "+poi.point.lat);
33. map.centerAndZoom(poi.point, 8);
34. });
35. localSearch.search(keyword);
36.
37.}
38.</script>
39.</html>
(源于http://tuzwu.iteye.com/blog/682699)
[解决办法]

JScript code
//同步坐标        function synchronizationCoordinate() {            var url = "http://maps.google.com/maps/api/geocode/json?address=" + encodeURIComponent($('#<%= txtAddress.ClientID %>').val()) + "&sensor=false" + "&randomNum=" + Math.random();            $.ajax({                url: url,                dataType: 'json',                success: function(data) {                    if (data.status == 'OK') {//经度                        $('#<%= txtLongitude.ClientID %>').val(data.results[0].geometry.location.lng);//纬度                        $('#<%= txtLatitude.ClientID %>').val(data.results[0].geometry.location.lat);                    }                    else {                        alert("没找到你要查询的位置,请重新输入!");                    }                },                error: function() {                    alert("网络繁忙,请重试!");                }            });        }
[解决办法]
C# code
/**     * 根据地址返回经纬度     * @param addr     * @return 返回经纬度数据, latLng[0]经度,latLng[1]维度     */    public static String[] getCoordinate(String addr) {        String[] latLng = new String[2];        String address = null;        try {            address = java.net.URLEncoder.encode(addr, "UTF-8");        } catch (UnsupportedEncodingException e1) {            e1.printStackTrace();        }        ;        String output = "csv";        //密钥可以随便写一个key=abc        String key = "abc";        String url = "http://maps.google.com/maps/geo?q=" + address + "&output=" + output + "&key=" + key;        URL googleMapURL = null;        URLConnection httpsConn = null;        // 进行转码        try {            googleMapURL = new URL(url);        } catch (MalformedURLException e) {            e.printStackTrace();        }        try {            httpsConn = (URLConnection)googleMapURL.openConnection();            if (httpsConn != null) {                InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");                BufferedReader br = new BufferedReader(insr);                String data = null;                if ((data = br.readLine()) != null) {                    String[] retList = data.split(",");                    /*                     * String latitude = retList[2]; String longitude =                     * retList[3];                     *                      * System.out.println("纬度"+ latitude);                     * System.out.println("经度"+ longitude);                     */                    if (retList.length > 2 && ("200".equals(retList[0]))) {                        latLng[0] = retList[2];                        latLng[1] = retList[3];                    }                }                insr.close();            }        } catch (IOException e) {            e.printStackTrace();        }        return latLng;    } 


[解决办法]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetLatLng.aspx.cs" Inherits="WebUI.GetLatLng" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>地理位置坐标转换-根据地址查询出经纬度</title>

<script src="http://ditu.google.cn/maps?file=api&amp;v=2&amp;key=your key&sensor=true"
type="text/javascript"></script>

<script type="text/javascript">
window.g = {};
window.$ = function(id) { return document.getElementById(id) };
window.onload = function() {
if (GBrowserIsCompatible()) {

g.map = new GMap2($("map"));
g.map.addControl(new GLargeMapControl());
g.map.addControl(new GMapTypeControl());
g.map.addControl(new GScaleControl());

g.geocoder = new GClientGeocoder();

g.getCoordinates = function(address) {
g.geocoder.getLatLng(
address,
function(point) {
if (point) {
g.map.setCenter(point, 13);
var marker = new GMarker(point);
g.map.addOverlay(marker);
var info = "<strong>" + address + "</strong><br />坐标: 纬度=" + point.lat() + ",经度=" + point.lng();
$("info").innerHTML = info;
marker.openInfoWindowHtml(info);
marker.__address_info = info;
GEvent.addListener(marker, "click", function() {
g.map.setCenter(this.getLatLng());
this.openInfoWindowHtml(this.__address_info);
$("info").innerHTML = info;
});
}
else {
alert("无法解析: " + address);
}
}
)
}

$("btn_go").onclick = function() {
g.getCoordinates($("address").value);
}
$("btn_go").onclick();
}
else {
alert('不支持的浏览器');
}
}
window.onunload = function() {
GUnload();
}
</script>

<style media="screen">
body
{
margin: 0;
padding: 0;
font-size: 9pt;
line-height: 1.5em;
}
#frame
{
width: 700px;
margin: 20px auto 10px;
}
#form
{
margin: 0 0 10px;
text-align: center;
}
#form input
{
border: 1px solid #ccc;
font-size: 9pt;
width: 200px;
}
#form button
{
font-size: 9pt;
border: 1px solid #ccc;
}
#form button:hover
{
background: #eef;
}
#map
{
height: 400px;
margin: 0 0 10px;
border: 5px solid #ccc;


}
</style>
</head>
<body>
<div id="frame">
<div id="form">
输入一个地址:
<input id="address" value="上海市浦东新区张江镇" />
<button id="btn_go">
获取坐标</button>
</div>
<div id="map">
</div>
<div id="info">
</div>
</div>
</body>
</html>

热点排行