显示谷歌地图onclick事件 - 谷歌地图API

时间:2015-01-28 21:36:09

标签: javascript mysql google-maps google-maps-api-3

我正在使用谷歌地图API来显示用户在地图上的位置。当initMap()触发onload事件时,用户会在页面加载时看到谷歌地图的一部分。但我想要的只是显示地图onclick按钮的sub_map

为了获得理想的结果,这是我尝试过的,但它没有用。我在initMap() onclick按钮后调用了sub_map

<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);



}

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

我最终得到了一个解决方案,它给了我想要的结果。我做的是,像往常一样调用initMap()函数onload事件,但是hide map首先使用visibility:hidden。当sub_map按钮为clicked时,map visibility将会显示。

$lee="<script>document.getElementById('map').style.visibility=\"visible\"</script>";