我一直试图解决这个问题差不多2个小时,我在Google和Stackoverflow上都找到了很多例子,但是他们要么不为我工作,要么不是我想要的。
我想要什么?我希望地图以包含城市的H6标签内的值(城市名称)为中心。此H6的ID为#City。
我尝试了什么? 1例:
center on a country by name google maps api v3
浏览器不喜欢上面链接中的以下代码:map.setCenter(results[0].geometry.location);
这会引发错误:
位置未定义
<h6 id="City">Mijas Costa</h6>
<div id="map"></div>
<style>
#map {
height: 400px;
width: 100%;
}
</style>
<script>
function initMap() {
// Styles a map in night mode.
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 52.3702157,
lng: 4.895167899999933
},
zoom: 12,
styles: [{
elementType: 'geometry',
stylers: [{
color: '#242f3e'
}]
},
{
elementType: 'labels.text.stroke',
stylers: [{
color: '#242f3e'
}]
},
{
elementType: 'labels.text.fill',
stylers: [{
color: '#746855'
}]
},
{
featureType: 'administrative.locality',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
},
{
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
},
{
featureType: 'poi.park',
elementType: 'geometry',
stylers: [{
color: '#263c3f'
}]
},
{
featureType: 'poi.park',
elementType: 'labels.text.fill',
stylers: [{
color: '#6b9a76'
}]
},
{
featureType: 'road',
elementType: 'geometry',
stylers: [{
color: '#38414e'
}]
},
{
featureType: 'road',
elementType: 'geometry.stroke',
stylers: [{
color: '#212a37'
}]
},
{
featureType: 'road',
elementType: 'labels.text.fill',
stylers: [{
color: '#9ca5b3'
}]
},
{
featureType: 'road.highway',
elementType: 'geometry',
stylers: [{
color: '#746855'
}]
},
{
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [{
color: '#1f2835'
}]
},
{
featureType: 'road.highway',
elementType: 'labels.text.fill',
stylers: [{
color: '#f3d19c'
}]
},
{
featureType: 'transit',
elementType: 'geometry',
stylers: [{
color: '#2f3948'
}]
},
{
featureType: 'transit.station',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
},
{
featureType: 'water',
elementType: 'geometry',
stylers: [{
color: '#17263c'
}]
},
{
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [{
color: '#515c6d'
}]
},
{
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [{
color: '#17263c'
}]
}
]
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAWwQ0Nm5wL4MqqV_o5VIroxzBpzmMjXuw&callback=initMap">
</script>
&#13;
感谢您的帮助。
答案 0 :(得分:2)
在geocoder.geocode
结果处理程序中status == google.maps.GeocoderStatus.OK
只执行:
map.setCenter(results[0].geometry.location);
这是一个完整的代码段:
$(document).ready(function() {
$('#search').on('click', function() {
geocoding();
});
});
var map, geocoder;
function initialize() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {
lat: 52.3702157,
lng: 4.895167899999933
},
zoom: 12,
styles: [{
elementType: 'geometry',
stylers: [{
color: '#242f3e'
}]
}, {
elementType: 'labels.text.stroke',
stylers: [{
color: '#242f3e'
}]
}, {
elementType: 'labels.text.fill',
stylers: [{
color: '#746855'
}]
}, {
featureType: 'administrative.locality',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'poi',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'poi.park',
elementType: 'geometry',
stylers: [{
color: '#263c3f'
}]
}, {
featureType: 'poi.park',
elementType: 'labels.text.fill',
stylers: [{
color: '#6b9a76'
}]
}, {
featureType: 'road',
elementType: 'geometry',
stylers: [{
color: '#38414e'
}]
}, {
featureType: 'road',
elementType: 'geometry.stroke',
stylers: [{
color: '#212a37'
}]
}, {
featureType: 'road',
elementType: 'labels.text.fill',
stylers: [{
color: '#9ca5b3'
}]
}, {
featureType: 'road.highway',
elementType: 'geometry',
stylers: [{
color: '#746855'
}]
}, {
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [{
color: '#1f2835'
}]
}, {
featureType: 'road.highway',
elementType: 'labels.text.fill',
stylers: [{
color: '#f3d19c'
}]
}, {
featureType: 'transit',
elementType: 'geometry',
stylers: [{
color: '#2f3948'
}]
}, {
featureType: 'transit.station',
elementType: 'labels.text.fill',
stylers: [{
color: '#d59563'
}]
}, {
featureType: 'water',
elementType: 'geometry',
stylers: [{
color: '#17263c'
}]
}, {
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [{
color: '#515c6d'
}]
}, {
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [{
color: '#17263c'
}]
}]
});
geocoder = new google.maps.Geocoder();
var city = $("#City").html();
document.getElementById('address').value = city;
geocoding(city);
}
function geocoding(address) {
if (!address)
address = document.getElementById('address').value;
geocoder.geocode({
'address': 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 error: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
height: 100%;
margin: 0;
padding: 0;
}
#geocode {
position: absolute;
top: 8px;
left: 50%;
margin-left: -180px;
z-index: 15;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<h6 id="City">Mijas Costa</h6>
<div id="geocode">
<input id="address" type="textbox" value="" />
<input id='search' type="button" value="search" />
</div>
<div id="map"></div>