无法在谷歌地图上添加标记

时间:2014-07-03 02:34:46

标签: javascript jquery maps markers

YO! 'Sup人:)我是html的新手,所以我可以使用一些帮助:) 我一直试图在页面顶部放置一个谷歌地图,具体高度:500 / 600px和宽度:100%。没问题。但是,当我尝试添加多个标记(作为URL链接n'自定义图标)时,它们不会显示..当我成功添加标记时我的(自动完成嵌入式)搜索栏消失:/有人请帮我做谷歌地图嵌入自动填充搜索栏和少数“链接”标记?告诉我,我做错了什么?我找了一个答案似乎无法找到它。提前谢谢..

如果有人不能读到这个(sry不知道如何格式化这个权利)这里是我的mega与html的链接。没有病毒,请不要威胁。

    <https://mega.co.nz/#!8lAi2I4Q!cPb3XJKccd54Qm8QtnBV2QBWt42KVDFzgI89ZnRCBJo>

这是来自html的相同代码:

<html>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false"
type="text/javascript"></script>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>                    <title>Garinfo</title>
<style>
html, body, #map-canvas {
height: 85%;
margin: 0px;
padding: 0px
}
.controls {
margin-top: 16px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#pac-input {
background-color: #fff;
padding: 0 11px 0 13px;
width: 400px;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
text-overflow: ellipsis;
}
#pac-input:focus {
border-color: #4d90fe;
margin-left: -1px;
padding-left: 14px;  /* Regular padding-left + 1. */
width: 401px;
}
.pac-container {
font-family: Roboto;
}
#type-selector {
color: #fff;
background-color: #4d90fe;
padding: 5px 11px 0px 11px;
}
#type-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places">            </script>
<script>
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(45.242629, 19.827195),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var input = /** @type {HTMLInputElement} */(
document.getElementById('pac-input'));
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(input);
var searchBox = new google.maps.places.SearchBox(
/** @type {HTMLInputElement} */(input));
google.maps.event.addListener(searchBox, 'places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}
markers = [];
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
map.fitBounds(bounds);
var location1 = new google.maps.Map.LatLng(45.239115, 19.824766)
var location2 = new google.maps.Map.LatLng(45.244517, 19.847312)
var marker1 = new google.maps.Marker({

position: Location1,
url: 'http://www.facebook.com/',
map: map
    });
var marker2 = new google.maps.Marker({
position: Location2,
url: 'http://www.google.com/',
map: map
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = marker.url;
});
markers.push(marker);
bounds.extend(place.geometry.location);
}
});
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
#target {
width: 345px;
}
</style>
</head>
<body onunload="GUnload()">
<script type="text/javascript">
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: new google.maps.LatLng(45.239115, 19.824766),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: map.getCenter(),
url: 'http://www.google.com/',
map: map
});
    google.maps.event.addListener(marker, 'click', function() {
window.location.href = marker.url;
});
</script>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map-canvas"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

人!

对代码进行分析和测试我发现至少有3个问题:

1)您要将地图两次添加到页面。看看第69和159行。

2)修复第112和113行:

var Location1 = new google.maps.Map.LatLng(45.239115, 19.824766);
var Location2 = new google.maps.Map.LatLng(45.244517, 19.847312);

他们错过了&#34 ;;&#34;并且变量名称与标记中使用的名称不匹配:

var marker1 = new google.maps.Marker({
  position: Location1,  //it should be written like this Location1
  url: 'http://www.facebook.com/',
  map: map
});

var marker2 = new google.maps.Marker({
  position: Location2, //it should be written like this Location2
  url: 'http://www.google.com/',
  map: map
});

3)未添加上述标记(1和2)。是的,它是tre。这可能是出于某些原因:

  • 3.1)第86行的函数places_changed可能没有被调用;
  • 3.2)places.length可能为0,使方法返回第90行;

运行上面的代码会直接添加两个标记,而不会混淆搜索字段(我已将其移至places_changed函数之外,正上方,并修复了javascript错误。)

可能你会发现更多问题,但必须先解决这个问题才能继续。