谷歌地图地理编码字符串 [英] Google Maps Geocoding a string
本文介绍了谷歌地图地理编码字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在以下位置完成了地理编码示例:
代码片段:
var Ireland = "Dublin";函数初始化(){地理编码器 = 新的 google.maps.Geocoder();var latlng = new google.maps.LatLng(53.3496, -6.3263);var mapOptions = {变焦:8,中心:latlng}map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);代码地址(爱尔兰);//调用函数}功能代码地址(地址){geocoder.geocode({地址:地址},功能(结果,状态){如果(状态 == google.maps.GeocoderStatus.OK){map.setCenter(results[0].geometry.location);//将地图居中放在结果上//在该位置放置一个标记var 标记 = 新的 google.maps.Marker({地图:地图,位置:结果[0].geometry.location});} 别的 {alert('地理编码失败,原因如下:' + status);}});}
html,身体,#地图画布{高度:100%;宽度:100%;填充:0;边距:0;}
<div id="map-canvas"></div><!-- 将 key 参数的值替换为您自己的 API 密钥.--><script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize"异步延迟></script>
I 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>
解决方案
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);
}
});
}
code snippet:
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);
}
});
}
html,
body,
#map-canvas {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
<div id="map-canvas"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize" async defer></script>
这篇关于谷歌地图地理编码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文