将谷歌地图居中到位置字符串
Center Google Maps to location string
使用Google Maps API,我想将地图居中为字符串而不是Lat/Lng
var mapOptions = {
center: "Paris"
};
您可以使用地理编码器。
工作代码片段:
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(0, 0);
var mapOptions = {
zoom: 8,
center: latlng
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
// Call the codeAddress function (once) when the map is idle (ready)
google.maps.event.addListenerOnce(map, 'idle', codeAddress);
}
function codeAddress() {
// Define address to center map to
var address = 'Paris, France';
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// Center map on location
map.setCenter(results[0].geometry.location);
// Add marker on 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);
}
});
}
initialize();
#map-canvas {
height: 180px;
}
<div id="map-canvas"></div>
<script src="//maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
JSFiddle 演示
编辑:当然,您可以像这样对完整的地址进行地理编码:
// Define address to center map to
var address = 'Rue Casse-Cul, Montboucher-sur-Jabron, France';
相关文章:
- 根据另一个数组对数组进行排序,包括位置和字符串[ES6]
- Javascript中的正则表达式,用于只有数字的字符串,并且不应在除数字以外的任何位置包含任何字符
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 在从索引位置返回的字符串中查找空白
- 为什么我在 webpack 中需要 html 文件时会得到一个位置字符串
- 如何在JavaScript中根据字符的位置提取子字符串
- 在光标位置的文本区域中插入一个字符串,并进行一些更改
- 通过位置栏更改查询字符串,AJAX加载的菜单不会't在没有硬刷新的情况下重新填充
- 如何在字符串中的特定位置插入变量
- javaScript更改字符串char's的位置
- 替换元素中字符串的所有位置
- javascript/谷歌地图:将位置对象转换为字符串
- 存储字母表中每个字母在所需位置的字符串中出现的次数
- 如何将字符串(包含位置)转换为数组
- 匹配字符串中任意位置的 3 个字符的正则表达式
- 将谷歌地图居中到位置字符串
- 使用 jQuery 从字符串中获取倒数第二个字符的位置
- FF 13,IE 9:JSON 字符串化/地理位置对象
- 字符串中的多边形位置
- Javascript dom连接背景位置字符串/自动更新背景图像