Javascript 居中地图并设置默认位置
我想把谷歌地图标记居中,我还想让它在没有点击事件发生时显示一个默认位置(在本例中是班加罗尔)。我在哪里可以换Javascript 居中地图并设置默认位置,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我想把谷歌地图标记居中,我还想让它在没有点击事件发生时显示一个默认位置(在本例中是班加罗尔)。我在哪里可以换 var locations = [ [ "BANGALORE", "215 West Girard Avenue 19123", "12.9716", "77.5946" ], [ "JAIPUR", "5360 Old York Road 19141", "26.9124", "75.7873"
var locations = [
[
"BANGALORE",
"215 West Girard Avenue 19123",
"12.9716",
"77.5946"
],
[
"JAIPUR",
"5360 Old York Road 19141",
"26.9124",
"75.7873"
],
[
"MUMBAI",
"1350 W Girard Avenue 19123",
"19.0760",
"72.8777"
],
[
"HYDERABAD",
"1950 W Girard Avenue 19123",
"17.3850",
"78.4867"
],
[
"CHENNAI",
"1950 W Girard Avenue 19123",
"13.0827",
"78.4867"
]
];
gmarkers = [];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(39.9995601, -75.1395161),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
function createMarker(latlng, html) {
var marker = new google.maps.Marker({
position: latlng,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
});
return marker;
}
for (var i = 0; i < locations.length; i++) {
gmarkers[locations[i][0]] =
createMarker(new google.maps.LatLng(locations[i][2], locations[i][3]), locations[i][0] + "<br>" + locations[i][1]);
}
var位置=[
[
“班加罗尔”,
“西吉拉德大道215号19123”,
"12.9716",
"77.5946"
],
[
“斋浦尔”,
“老约克路5360号19141”,
"26.9124",
"75.7873"
],
[
“孟买”,
“19123年吉拉德大道西1350号”,
"19.0760",
"72.8777"
],
[
“海得拉巴”,
“1950 W吉拉德大道19123号”,
"17.3850",
"78.4867"
],
[
“钦奈”,
“1950 W吉拉德大道19123号”,
"13.0827",
"78.4867"
]
];
gmarkers=[];
var map=new google.maps.map(document.getElementById('map'){
缩放:12,
中心:新google.maps.LatLng(39.9995601,-75.1395161),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
函数createMarker(latlng,html){
var marker=new google.maps.marker({
位置:latlng,
地图:地图
});
google.maps.event.addListener(标记'click',函数(){
setContent(html);
信息窗口。打开(地图、标记);
});
返回标记;
}
对于(变量i=0;i”+locations[i][1]);
}
链接到小提琴:
click
事件var位置=[
[
“班加罗尔”,
“西吉拉德大道215号19123”,
"12.9716",
"77.5946"
],
[
“斋浦尔”,
“老约克路5360号19141”,
"26.9124",
"75.7873"
],
[
“孟买”,
“19123年吉拉德大道西1350号”,
"19.0760",
"72.8777"
],
[
“海得拉巴”,
“1950 W吉拉德大道19123号”,
"17.3850",
"78.4867"
],
[
“钦奈”,
“1950 W吉拉德大道19123号”,
"13.0827",
"78.4867"
]
];
gmarkers=[];
var map=new google.maps.map(document.getElementById('map'){
缩放:12,
中心:新google.maps.LatLng(12.971677.5946),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
函数createMarker(latlng,html){
var marker=new google.maps.marker({
位置:latlng,
地图:地图
});
google.maps.event.addListener(标记'click',函数(){
setContent(html);
信息窗口。打开(地图、标记);
map.setCenter(this.getPosition())
});
返回标记;
}
对于(变量i=0;i”+locations[i][1]);
}
//将默认值设置为班加罗尔
google.maps.event.trigger(gmarkers['BANGALORE',“点击”)代码>
#第23节{
背景:白色;
填充:80px;
文本对齐:居中;
}
#第23课{
右边填充:20px;
左侧填充:20px;
}
#第23A条{
文本对齐:居中;
字体大小:20px;
字体大小:粗体;
字体系列:“ProximaNova Bold”;
文字装饰:无;
颜色:黑色;
填充:30px;
}
.clickableDiv{
边框:黑色1px实心;
}
#地图{
宽度:100%;
高度:450px;
位置:相对位置;
}
谢谢,这很有帮助,但它只会在我没有在单击时触发任何内容时集中到默认位置。当我单击其他位置时,它们不会居中。非常感谢,这解决了问题。你解释得很好:)
// Marker code starts
var infowindow = new google.maps.InfoWindow();
var mylatlng = new google.maps.LatLng(39.9995601,-75.1395161);
var marker = new google.maps.Marker({
position: mylatlng,
map: map
});
//Create marker function goes here
//Set default to Bangalore
google.maps.event.trigger(gmarkers['BANGALORE'],"click");
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
map.setCenter(this.getPosition())
});