0

私は Google Maps Api のストア ファインダーとうまくいかないので (私の他の未回答の不満を参照)、別の方法で対処しようとしました: php-mysql-query と xml-file を介してすべての場所をマップに入れ、テキスト入力で自分の場所を検索します。それぞれのジョブは個別にうまく動作しますが、結合すると自分の位置のマーカーが得られません。つまり、ジオココーダーが正しく動作していないようです。BUT:ナンセンスアドレスを入力すると、正しいエラーメッセージが表示されます。そのため、ジオコーディングのマーカーが表示されないように思えますが、理由はわかりません。誰か助けてくれませんか?これが例 (http://umwelt-und-information.com/maps/ASPE_Adressen_zeigen.php) で、コードは次のとおりです。

<?php
//error_reporting(E_ALL);    
require("dbpass.php");
if (PHP_VERSION>='5')
require_once('domxml-php4-to-php5.php');
// Opens a connection to a mySQL server
$connection=mysql_connect ($dbhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);
// Select all the rows in the ASPE table
$query = "SELECT DISTINCT Nachname, lng, lat, Strasse, Telefon_P, Telefon_D, Telefon_M FROM ASPE";
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $doc->create_element("marker");
$newnode = $parnode->append_child($node);
$newnode->set_attribute("Nachname", $row['Nachname']);
$newnode->set_attribute("Strasse ", $row['Strasse']);
$newnode->set_attribute("Telefon_P", $row['Telefon_P']);
$newnode->set_attribute("Telefon_D", $row['Telefon_D']);
$newnode->set_attribute("Telefon_M", $row['Telefon_M']);
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
}
$xmlfile = $doc->dump_file("ASPE.xml");
mysql_close($connection);
//header("Location: http://umwelt-und-information.com/maps/kartenansicht_Art.php"); 
?>

<script src="http://maps.google.com/maps/api/js?sensor=false" 
   type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var map;
var geocoder;
var markers = [];
var infoWindow;
var iconimage = 'ok.png';

// Geocoder
geocoder = new google.maps.Geocoder();
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    // map.setZoom(15);
    var iconimage2 = 'correct.png';
    var marker = new google.maps.Marker({
        map: map,
        icon: iconimage2,
        position: results[0].geometry.location
    });
  } else {
    alert("Zu Ihren Angaben konnte kein Fundort gefunden werden: " + status);
  }
});
}

function load() {
    var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(52.6145, 8.3418),
    zoom: 13,
    mapTypeId: 'roadmap',
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
  });
  infoWindow = new google.maps.InfoWindow();






    downloadUrl("ASPE.xml?ienocache="+new Date().getMilliseconds(), function(data){ 
       var xml = data.responseXML; 
      var markers = xml.documentElement.getElementsByTagName("marker");
      var bounds = new google.maps.LatLngBounds();
      for (var i = 0; i < markers.length; i++) {
        var Nachname = markers[i].getAttribute("Nachname");
        var Strasse = markers[i].getAttribute("Strasse");
        var Telefon_P = markers[i].getAttribute("Telefon_P");
        var Telefon_D = markers[i].getAttribute("Telefon_D");
        var Telefon_M = markers[i].getAttribute("Telefon_M");
        var latlng = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng")));

       var html = "<b>" + "Nachname: "  + "</b>" + Nachname +  " <br/>" +
         "<b>" + "Strasse: " +  "</b>" + Strasse + "<br/>" + 
         "<b>" + "Telefon (p): " + "</b>" + Telefon_P + "<br/>" +
         "<b>" + "Telefon (d):" +  "</b>" + Telefon_D + "<br/>" +
         "<b>" + "Telefon (m):" +  "</b>" + Telefon_M + "<br/>";
        var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        icon: iconimage         
        });     


   bounds.extend(latlng);
   map.fitBounds(bounds);
   var listener = google.maps.event.addListener(map, "idle", function() { 
    if (map.getZoom() > 15) map.setZoom(15); 
    google.maps.event.removeListener(listener); 
    });




 bindInfoWindow(marker, map, infoWindow, html);
 }
 });
 }           


function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open (map, marker);
});
markers.push(marker);
}



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, request.status);
    }
  };

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



function doNothing() {}

//]]>




</script>

</head>

<body onload="load()">
<div> Geben Sie hier eine Adresse zum leichteren Auffinden des Fundortes ein: <br> 
<input id="address" type="textbox" value="" size="80">
<input type="button" value="Fundort finden" onclick="codeAddress()"> <br></br></div>


<div id="map" style="width: 600px; height: 500px"></div>


</body>
4

2 に答える 2

0

これで、すべて正常に動作します (ジオコードのオートコンプリートを除きますが、それは重要ではありません) :-) mysql データベースから場所を表示し、アドレスを入力 (ジオコーディング) し、新しい場所をクリックしてマーカーを追加できます。 . http://umwelt-und-information.com/maps/ASPE_Adressen_zeigen.phpを参照してください。以前は機能しなかった理由がわかりました。「function load ()...」と「を使用しましたが、今では「function initialize ()」と「それが機能します!! ひょっとしたら、これら 2 つのオプションの違いについてコメントできる人がいるかもしれません... ;-) とにかく、コードは次のとおりです (誰かがそれを必要とする場合):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-    transitional.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /></head>
<?php
//error_reporting(E_ALL);    

require("dbpass.php");
if (PHP_VERSION>='5')
require_once('domxml-php4-to-php5.php');

// Opens a connection to a mySQL server

$connection=mysql_connect ($dbhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

 // Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Start XML file, create parent node
$doc = domxml_new_doc("1.0");
$node = $doc->create_element("markers");
$parnode = $doc->append_child($node);


// Select all the rows in the ASPE table
$query = "SELECT DISTINCT Nachname, Vorname, lng, lat, Strasse, Telefon_P, Telefon_D, Telefon_M FROM ASPE";
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$num_results = mysql_num_rows($result);
echo "<b> $num_results </b> Ergebnis(se) passt/passen zu Ihrer Abfrage</br>"; 


// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $doc->create_element("marker");
$newnode = $parnode->append_child($node);
$newnode->set_attribute("Vorname", utf8_encode ($row['Vorname']));
$newnode->set_attribute("Nachname", utf8_encode ($row['Nachname']));
$newnode->set_attribute("Strasse ", utf8_encode ($row['Strasse']));
$newnode->set_attribute("Telefon_P", utf8_encode ($row['Telefon_P']));
$newnode->set_attribute("Telefon_D", utf8_encode ($row['Telefon_D']));
$newnode->set_attribute("Telefon_M", utf8_encode ($row['Telefon_M']));
$newnode->set_attribute("lat", $row['lat']);
$newnode->set_attribute("lng", $row['lng']);
}
$xmlfile = $doc->dump_file("ASPE.xml");
mysql_close($connection);
//header("Location: http://umwelt-und-information.com/maps/kartenansicht_Art.php"); 

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

<script type="text/javascript">


//<![CDATA[
var map;
var geocoder;
var markers = [];
var infoWindow;


  // Geocoder

var input = document.getElementById('searchTextField');
//var options = {
types: ['(geocode)']
//componentRestrictions: {country: 'de'}
//};

autocomplete = new google.maps.places.Autocomplete(input, options);


function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    map.setZoom(15);
    var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
    });
  } else {
    alert("Zu Ihren Angaben konnte kein Fundort gefunden werden: " + status);
  }
});
}

function initialize() {
var latlng = new google.maps.LatLng(51.5, 7.5);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

geocoder = new google.maps.Geocoder();  
infoWindow = new google.maps.InfoWindow();


var marker;
function placeMarker(location) {
if ( marker ) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
  position: location,
  map: map
});
}
}

google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});             

    downloadUrl("ASPE.xml?ienocache="+new Date().getMilliseconds(), function(data){ 
       var xml = data.responseXML; 
      var markers = xml.documentElement.getElementsByTagName("marker");
      var bounds = new google.maps.LatLngBounds();
      for (var i = 0; i < markers.length; i++) {
        var Vorname = markers[i].getAttribute("Vorname");
        var Nachname = markers[i].getAttribute("Nachname");
        var Strasse = markers[i].getAttribute("Strasse");
        var Telefon_P = markers[i].getAttribute("Telefon_P");
        var Telefon_D = markers[i].getAttribute("Telefon_D");
        var Telefon_M = markers[i].getAttribute("Telefon_M");
        var latlng = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("lat")),
            parseFloat(markers[i].getAttribute("lng")));

       var html = 
       "<b>" + "Vorname: "  + "</b>" + Vorname +  " <br/>" +
       "<b>" + "Nachname: "  + "</b>" + Nachname +  " <br/>" +
         "<b>" + "Strasse: " +  "</b>" + Strasse + "<br/>" + 
         "<b>" + "Telefon (p): " + "</b>" + Telefon_P + "<br/>" +
         "<b>" + "Telefon (d):" +  "</b>" + Telefon_D + "<br/>" +
         "<b>" + "Telefon (m):" +  "</b>" + Telefon_M + "<br/>";
        var iconimage2 = 'correct.png';
        var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        icon: iconimage2         
        });     


  bounds.extend(latlng);
  map.fitBounds(bounds);
  var listener = google.maps.event.addListener(map, "idle", function() { 
  if (map.getZoom() > 15) map.setZoom(15); 
  google.maps.event.removeListener(listener); 
  });




bindInfoWindow(marker, map, infoWindow, html);
}
});
}           


function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open (map, marker);
});
markers.push(marker);
}



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, request.status);
    }
  };

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



 function doNothing() {}

 //]]>




  </script>

</head>


<div> Geben Sie hier die Adresse des Fundortes ein: <br> <input id="address" type="textbox" value="" size="80">
<input type="button" value="Fundort finden" onclick="codeAddress()"> <br></br></div>
<body onload="initialize()">

<div id="map_canvas" style="width: 800px; height: 600px"></div>


</body>
</html>
于 2012-09-23T19:41:17.830 に答える
0

18行目:

 var map = new google.maps.Map(document.getElementById("map"), {

load() 関数の外で宣言するようにしてください。そうしないと、Firebug が 103 行目でエラー (map undefined) を取得するためです。

于 2012-09-18T10:50:29.903 に答える