Google地图对一个字符串进行地理编码 [英] Google Maps Geocoding a string
问题描述
im google新增了Google地图api并完成了地理编码示例:
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
我希望能够对代码中的字符串进行地理编码,并在该位置放置标记,而不是用户搜索位置。
我的代码到目前为止是:
<!DOCTYPE html>
< html>
< head>
< meta name =viewportcontent =initial-scale = 1.0,user-scalable = no>
< meta charset =utf-8>
< title>地理编码服务< / title>
< style>
html,body,#map-canvas
{
height:100%;
margin:0px;
填充:0px
}
< / style>
< script src =https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false>< / script>
< script>
//全局变量
var geocoder;
var map;
var Ireland =都柏林;
函数initialize()
{
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(53.3496,-6.3263);
var mapOptions =
{
zoom:8,
center:latlng
}
map = new google.maps.Map(document.getElementById(' map-canvas'),mapOptions);
codeAddress(); //调用函数
}
函数codeAddress()
{
var address = document.getElementById(Ireland) 。值;
geocoder.geocode({Ireland:address},function(results,status)
{
if(status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results [0] .geometry.location); //将结果放在结果中心
//在位置放置一个标记
var marker = new google.maps.Marker (
{
map:map,
position:results [0] .geometry.location
});
} else {
alert('Geocode由于以下原因而不成功:'+ status';
}
});
}
google.maps.event.addDomListener(window,'load',initialize);
< / script>
< / head>
< body>
< div id =map-canvas>< / div>
< / body>
< / html>
这可能很简单,但正如我所说的即时通讯新的谷歌地图和HTML,并会爱任何帮助所以请不要投下。
谢谢
这将改变codeAddress函数以采用参数并对该参数进行地理编码。如果您传递爱尔兰变量作为参数,它会将地理编码Dublin。
var Ireland =Dublin;
函数initialize()
{
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(53.3496,-6.3263);
var mapOptions =
{
zoom:8,
center:latlng
}
map = new google.maps.Map(document.getElementById(' map-canvas'),mapOptions);
codeAddress(Ireland); //调用函数
函数codeAddress(address)
{
geocoder.geocode({address:address} ,函数(结果,状态)
{
if(status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results [0] .geometry.location ); //将地图放在结果中心
//在地点放置一个标记
var marker = new google.maps.Marker(
{
map:map,
position:results [0] .geometry.location
});
} else {
alert('由于以下原因,地理编码不成功:'+ status';
}
});
}
im new to google maps api and have done the geocoding example at:
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
I want to be able to geocode a string within my code and place a marker at the location, rather than a user search a location. my code so far is:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Geocoding service</title>
<style>
html, body, #map-canvas
{
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
//global variables
var geocoder;
var map;
var Ireland = "Dublin";
function initialize()
{
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(53.3496, -6.3263);
var mapOptions =
{
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
codeAddress();//call the function
}
function codeAddress()
{
var address = document.getElementById("Ireland").value;
geocoder.geocode( {"Ireland":address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);//center the map over the result
//place a marker at the location
var marker = new google.maps.Marker(
{
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
It's probably something simple but as i said im new to google maps and html and would love any help at all so please dont vote down. Thanks
This will change the codeAddress function to take an argument and geocode that argument. If you pass the Ireland variable as the argument it will geocode "Dublin".
var Ireland = "Dublin";
function initialize()
{
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(53.3496, -6.3263);
var mapOptions =
{
zoom: 8,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
codeAddress(Ireland);//call the function
}
function codeAddress(address)
{
geocoder.geocode( {address:address}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
map.setCenter(results[0].geometry.location);//center the map over the result
//place a marker at the location
var marker = new google.maps.Marker(
{
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
这篇关于Google地图对一个字符串进行地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!