我正在制作公司列表模板,并希望在Google地图中显示公司的位置。这是我第一次使用Google Maps API并且我的代码正常运行。但我知道我现在并没有以正确的方式做到这一点。
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var address = '<?php Here I give the address from my database by PHP ?>';
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
});
}
});
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 16,
center: latlng
}
map = new google.maps.Map(document.getElementById('big_map'), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="big_map"></div>
我想通过mapOptions
指定center
results[0].geometry.location
,但我无法让它发挥作用。
正如我所说,我的代码有效,但也许有人可以告诉我如何以更好的方式做到这一点,因为我知道这不是100%正确。
谢谢!
答案 0 :(得分:0)
您是否尝试更改订单以获取&#34; map&#34;,就像这样......
第一:
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 16,
center: latlng
}
map = new google.maps.Map(document.getElementById('big_map'), mapOptions);
第二
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
});
}
});
答案 1 :(得分:0)
我使用了这段代码,它起作用了:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 16,
center: latlng
}
map = new google.maps.Map(document.getElementById('big_map'), mapOptions);
//var address = '<?php Here I give the address from my database by PHP ?>';
geocoder.geocode( { 'address': 'Somewhere'}, 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 was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="big_map" class="googleMapsMunicipios"></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 16,
center: latlng
}
map = new google.maps.Map(document.getElementById('big_map'), mapOptions);
//var address = '<?php Here I give the address from my database by PHP ?>';
geocoder.geocode( { 'address': 'Somewhere'}, 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 was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="big_map" class="googleMapsMunicipios"></div>
欲了解更多信息,请访问: