1

我一直在开发 URL 的做法:https ://developers.google.com/maps/articles/phpsqlinfo_v3并且它可以工作,但在另一个示例中,我需要在单击显示的标记时出现相同的窗口 HTML 表单当我使用代码定位我的位置并将该信息保存到 MySQL 数据库中时映射。我正在使用下面的代码,但 HTML 表单窗口似乎没有保存地理位置数据。

这是网页中使用的代码:phpsqlinfo_add.html(URL 示例的类似名称:https ://developers.google.com/maps/articles/phpsqlinfo_v3 ) 其他补充网页:phpsqlinfo_addrow.php 和 phpsqlinfo_dbinfo.php 类似到例子。

代码:phpsqlinfo_add.html

 <!DOCTYPE html >
 <head>
 <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
 <title>:: DEMO ::</title>
 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
 <script type="text/javascript">

 function detectBrowser() {
 var useragent = navigator.userAgent;
 var mapdiv = document.getElementById("map_canvas");

 if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
 mapdiv.style.width = '100%';
 mapdiv.style.height = '100%';
 } else {
 mapdiv.style.width = '600px';
 mapdiv.style.height = '800px';
 }
 }

 function init() {
 if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(function(position) {
 var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
 var marcador = new google.maps.Marker();

 var map = new google.maps.Map(document.getElementById('map_canvas'), {
 zoom: 16,
 center: pos,
 mapTypeId: google.maps.MapTypeId.ROADMAP
 });

 marcador.setPosition(pos);
 marcador.setMap(map)

 }, function() {
 alert("Su navegador debe soportar geolocalizacion");
 });
 }
 }

 google.maps.event.addDomListener(window, 'load', init);

 var map = new google.maps.Map(document.getElementById("map_canvas"), options);

 var html = "<table>" +
             "<tr><td>Nombre:</td> <td><input type='text' id='name' value='Juan Gomez' readonly='readonly' /> </td> </tr>" +
             "<tr><td>Direccion:</td> <td><input type='text' id='address'/></td> </tr>" +
             "<tr><td>Tipo:</td> <td><select id='type'>" +
             "<option value='Opcion 1' SELECTED>Opcion 1</option>" +
             "<option value='Opcion 2'>Opcion 2</option>" +
             "<option value='Opcion 3'>Opcion 3</option>" +
             "</select> </td></tr>" +
             "<tr><td></td><td><input type='button' value='Registrar' onclick='saveData()'/></td></tr>";
infowindow = new google.maps.InfoWindow({
content: html
});

 google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map_canvas
    });
    google.maps.event.addListener(marker, "click", function() {
      infowindow.open(map, marker);
    });
});


 function saveData() {
 var name = escape(document.getElementById("name").value);
 var address = escape(document.getElementById("address").value);
 var type = document.getElementById("type").value;
 var latlng = marker.getPosition();

 var url = "phpsqlinfo_addrow.php?name=" + name + "&address=" + address +
            "&type=" + type + "&lat=" + latlng.lat() + "&lng=" + latlng.lng();
  downloadUrl(url, function(data, responseCode) {
    if (responseCode == 200 && data.length <= 1) {
      infowindow.close();
      document.getElementById("message").innerHTML = "Ubicacion Registrada.";
    }
  });
}

function downloadUrl(url, callback) {
 var request = window.ActiveXObject ?
 new ActiveXObject('Microsoft.XMLHTTP') :
 new XMLHttpRequest;

 request.onreadystatechange = function() {
 if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
 };

 request.open('GET', url, true);
 request.send(null);
 }

 function doNothing() {}
 </script>

 </head>

 <body style="margin:0px; padding:0px;" onLoad="initialize()">
 <div id="map_canvas" style="width: 500px; height: 300px"></div>
 <div id="message"></div>
 </body>

 </html>
4

1 回答 1

0

添加 onclick 事件侦听器和创建信息窗口的代码从未被激活,而且您引用的是 map_canvas div 而不是您的地图。试试这个(请参阅// * my comments *更改):

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">

function detectBrowser() {
var useragent = navigator.userAgent;
var mapdiv = document.getElementById("map_canvas");

if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
mapdiv.style.width = '100%';
mapdiv.style.height = '100%';
} else {
mapdiv.style.width = '600px';
mapdiv.style.height = '800px';
}
}

function init() {
// * adjusted geolocation code for testing *
var pos = new google.maps.LatLng(51.511,0.275);
var marcador = new google.maps.Marker();
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 16,
center: pos,
mapTypeId: google.maps.MapTypeId.ROADMAP
});

marcador.setPosition(pos);
marcador.setMap(map)



// * infowindow code wasn't being activated, moved inside init() function *
var html = "<table>" +
         "<tr><td>Nombre:</td> <td><input type='text' id='name' value='Juan Gomez' readonly='readonly' /> </td> </tr>" +
         "<tr><td>Direccion:</td> <td><input type='text' id='address'/></td> </tr>" +
         "<tr><td>Tipo:</td> <td><select id='type'>" +
         "<option value='Opcion 1' SELECTED>Opcion 1</option>" +
         "<option value='Opcion 2'>Opcion 2</option>" +
         "<option value='Opcion 3'>Opcion 3</option>" +
         "</select> </td></tr>" +
         "<tr><td></td><td><input type='button' value='Registrar' onclick='saveData()'/></td></tr>";

infowindow = new google.maps.InfoWindow({
content: html
});

// * changed map: map_canvas to map: map *
 google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map
    });
    google.maps.event.addListener(marker, "click", function() {
      infowindow.open(map, marker);
    });
});
google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map
    });

    google.maps.event.addListener(marker, "click", function() {
      infowindow.open(map, marker);
    });
});
}



google.maps.event.addDomListener(window, 'load', init);

// * don't need to create map again *
// var map = new google.maps.Map(document.getElementById("map_canvas"), options);

// * rest of code should be fine... *

还要小心<body onLoad="initialize()">andgoogle.maps.event.addDomListener(window, 'load', init);如果这不是一个错误并且 initialize() 做了一些不同的事情,那么将 init() 和 initialize() 结合起来会更好。

于 2013-03-30T11:04:57.130 回答