请问Google Map API怎么根据地址获得经纬度?

huangama2011 2011-08-15 09:59:45
请问Google Map API怎么根据地址获得经纬度?
根据输入的地址,获得该地址的经纬度?
求代码
...全文
3110 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
w419758256 2013-09-10
  • 打赏
  • 举报
回复
我也遇到问题@wxr0323,用这个方法,我申请了key值,但调用结果还是不行,返回code610,是说key值无效~~~~~~~~有谁帮我解决下啊,急需解决。我Q是419758256 真的很急
琳芯蓝 2012-04-09
  • 打赏
  • 举报
回复
不会 我也遇上同样的问题了 求解
  • 打赏
  • 举报
回复
<%@ 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&v=2&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>
Lay 2011-08-15
  • 打赏
  • 举报
回复

//同步坐标
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("网络繁忙,请重试!");
}
});
}
LMAOhuaNL 2011-08-15
  • 打赏
  • 举报
回复
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)
子夜__ 2011-08-15
  • 打赏
  • 举报
回复
/**
* 根据地址返回经纬度
* @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;
}

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧