显示 google 地图 onclick 事件 - Google 地图 API
Show google map onclick event - Google Map API
我正在使用 google 地图 API 显示用户在 map.As 上的位置 initMap()
触发 onload
事件,用户看到 [=41] 的一部分=] 当页面 loads.But 我想要的是地图时,只显示 sub_map
按钮的地图 onclick
。
为了得到想要的结果,这是我尝试过的方法,但它没有在 sub_map
按钮 onclick
之后 work.I 调用 initMap()
<form ><input type='submit' id='sub_map' name='sub_map' onclick='open_map(".$userx.");' value='See Map'></form>";
Javascript
function open_map(x){
var user_map=x;
initMap();
var ajax=new XMLHttpRequest();
ajax.open("post","search_results.php",true);
ajax.setRequestHeader("content-type","application/x-www-form-urlencoded");
ajax.onreadystatechange=function(){
if(ajax.readyState == 4 && ajax.status == 200){
document.getElementById('map').innerHTML=ajax.responseText;
}
}
ajax.send("user_map="+user_map);
}
Google 地图部分
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
new google.maps.Size(40, 40), new google.maps.Point(0,0),
new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
//map.panBy(0,30);
//map.setZoom(15);
center: new google.maps.LatLng(0,0),
zoom: 1,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
});
<?php
if(isset($_POST['user_map'])){
$mapusrx=$_POST['user_map'];
$query = mysqli_query($connecti,"SELECT * FROM map WHERE user_id='$mapusrx'");
while ($row = mysqli_fetch_assoc($query)){
$name=$row['title'];
$lat=$row['lat'];
$lon=$row['lon'];
$desc=$row['address'];
echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");
}
}
?>
center = bounds.getCenter();
map.setCenter(center);
map.setZoom(16);
}
感谢您的宝贵时间。
我最终得到了一个解决方案,它给了我想要的 result.What 我所做的是,像往常一样调用 initMap()
函数 onload
事件,但是 hide
map
首先使用 visibility:hidden
。当 sub_map
按钮为 clicked
时,map visibility
将可见。
$lee="<script>document.getElementById('map').style.visibility=\"visible\"</script>";
我正在使用 google 地图 API 显示用户在 map.As 上的位置 initMap()
触发 onload
事件,用户看到 [=41] 的一部分=] 当页面 loads.But 我想要的是地图时,只显示 sub_map
按钮的地图 onclick
。
为了得到想要的结果,这是我尝试过的方法,但它没有在 sub_map
按钮 onclick
之后 work.I 调用 initMap()
<form ><input type='submit' id='sub_map' name='sub_map' onclick='open_map(".$userx.");' value='See Map'></form>";
Javascript
function open_map(x){
var user_map=x;
initMap();
var ajax=new XMLHttpRequest();
ajax.open("post","search_results.php",true);
ajax.setRequestHeader("content-type","application/x-www-form-urlencoded");
ajax.onreadystatechange=function(){
if(ajax.readyState == 4 && ajax.status == 200){
document.getElementById('map').innerHTML=ajax.responseText;
}
}
ajax.send("user_map="+user_map);
}
Google 地图部分
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
new google.maps.Size(40, 40), new google.maps.Point(0,0),
new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
//map.panBy(0,30);
//map.setZoom(15);
center: new google.maps.LatLng(0,0),
zoom: 1,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
});
<?php
if(isset($_POST['user_map'])){
$mapusrx=$_POST['user_map'];
$query = mysqli_query($connecti,"SELECT * FROM map WHERE user_id='$mapusrx'");
while ($row = mysqli_fetch_assoc($query)){
$name=$row['title'];
$lat=$row['lat'];
$lon=$row['lon'];
$desc=$row['address'];
echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");
}
}
?>
center = bounds.getCenter();
map.setCenter(center);
map.setZoom(16);
}
感谢您的宝贵时间。
我最终得到了一个解决方案,它给了我想要的 result.What 我所做的是,像往常一样调用 initMap()
函数 onload
事件,但是 hide
map
首先使用 visibility:hidden
。当 sub_map
按钮为 clicked
时,map visibility
将可见。
$lee="<script>document.getElementById('map').style.visibility=\"visible\"</script>";