GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
PROBLEME MIT findNearbyPostalCodesJSON  XML
Forum Index -> Forum in Deutsch
Author Message
Anonymous



Wollte meine DB nach Ewents abfragen die neher als 50 km sind
da die findNearbyPostalCodesJSON Abfrage nach 500 Ergebnisse stehen bleibt und ich nicht nach dessen ergebniss meine DB durchsunchen kann hoffe ich auf diesem weg eine Lösung zu finden.

Das Problem kann man anhand des skripts sehen:
Code:
 
 <html>
 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">	
 <body>
 <form id="searchForm" onsubmit="search1(); return false;" style="display:inline">
 land =   <input id="land" type="hidden" name=land value="ch">
 <select name ="country" onchange="javascript:land.value=this.value"><option value=""> all countries</option><option value="AT"> Austria</option><option value="AU"> Australia</option><option value="BE"> Belgium</option><option value="CH" selected> Switzerland</option><option value="CZ"> Czech Republic</option>
 <option value="DE"> Germany</option></select><br>
 
    PLZ=<input id="iplz" type=text name=iplz value="8000">
    KM=<input id="ikm" type=text name=ikm value="50">
    <input type=submit value=searchplzungebung>
  </form><br>
 <div id="resultDiv"  style="position:absolute;top:100;left:0">PLZ und KM eingeben dann suchen</div>
  </body></html>
 
 <script type="text/javascript">
 function search1() {
 durchl=1;
  request = 'http://ws.geonames.org/findNearbyPostalCodesJSON?postalcode=' +  encodeURIComponent(document.getElementById("iplz").value)  + '&country=' +  encodeURIComponent(document.getElementById("land").value)  + '&radius=' +  encodeURIComponent(document.getElementById("ikm").value)  + '&maxRows=500&callback=getnplz'; 
  aObj = new JSONscriptRequest(request);
  aObj.buildScriptTag();
  aObj.addScriptTag();
  }
 
 function entfm1(lat1,lng1){
 lat2=eingcorx;lng2=eingcory;
  erdradiusKM = 6372.0;    //durchschnittswert
  erdradius_MILES = 3963.0;
 		a1 = 2*Math.PI*lat1/360;b1 = 2*Math.PI*lng1/360
     		a2 = 2*Math.PI*lat2/360;b2 = 2*Math.PI*lng2/360
     		d = Math.acos(Math.cos(a1) * Math.cos(b1) * Math.cos(a2) * Math.cos(b2) + Math.cos(a1) * Math.sin(b1) * Math.cos(a2) * Math.sin(b2) + Math.sin(a1) * Math.sin(a2));
     		er= d * erdradiusKM;
 //    		erMiles= d * erdradiusKM*erdradius_MILES/erdradiusKM //Fals angaben in miles benötigt
     }
 
 
 
 function getnplz(dbnplz) {   				 //Antwort abfangen
  if (dbnplz == null) {  alert("null");  return;  }     //ist antwort null->abbruch
 dbnplz = dbnplz.postalCodes		 //Daten abspeichern
    var html = '<u><b>PLZ = KM abstand</b></u><br>';
    eingcorx=dbnplz[0].lat
    eingcory=dbnplz[0].lng
    for (i=0;i< dbnplz.length;i++) {
       var name = dbnplz[i];
 	durchl++;if(durchl>=9){durchl=1;html = html+"<br>"}
       entfm1(name.lat,name.lng)
       er=Math.round(er*100)/100            // auf 2 Stellen Runden   
 //      html = html + '<a href="javascript:entfm1(' + name.lat +',' + name.lng + ');">' +name.placeName + name.postalCode+'</a> = '+er+'KM<br>';
       html = html + name.postalCode + ' = ' + er + 'KM ; ';
 
    }
  document.getElementById('resultDiv').innerHTML = html;
  }
  </script>
 


Wenn ich meine DB nach den Koordinaten abfragen.
Kriege ich aber kein Kreiss sondern ein Viereck als Ergebniss.

Die lösung wehre über die Matrix abzufragen habs aber noch nicht geraft.
Ist jemand mit sowas vertraut?

d1 = Math.acos(Math.cos(a1) * Math.cos(b1) * Math.cos(a2) * Math.cos(b2) + Math.cos(a1) * Math.sin(b1) * Math.cos(a2) * Math.sin(b2) + Math.sin(a1) * Math.sin(a2));

d2 = Math.acos(Math.cos(a3) * Math.cos(b3) * Math.cos(a4) * Math.cos(b4) + Math.cos(a3) * Math.sin(b3) * Math.cos(a4) * Math.sin(b4) + Math.sin(a3) * Math.sin(a4));

if(d1>=d2-50km && d1<=d2+50km){alert("juhu es trift zu!")}



Bin um jede Hilfe dankbar. Bin schon am verzweifeln denn ich muss meine DB und die Einträge für die neue Abfrageart aktualisieren
Anonymous



damit das Obrige Beispiel funktioniert muss natürlich noch das jsr_class.js
script eingebunden werden:

Code:
 function JSONscriptRequest(fullUrl) {
     this.fullUrl = fullUrl; this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
     this.headLoc = document.getElementsByTagName("head").item(0);this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
 }
 JSONscriptRequest.scriptCounter = 1;
 JSONscriptRequest.prototype.buildScriptTag = function () {
     this.scriptObj = document.createElement("script");    this.scriptObj.setAttribute("type", "text/javascript");
     this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);    this.scriptObj.setAttribute("id", this.scriptId);
 }
  JSONscriptRequest.prototype.removeScriptTag = function () {    this.headLoc.removeChild(this.scriptObj); }
 JSONscriptRequest.prototype.addScriptTag = function () {  this.headLoc.appendChild(this.scriptObj);}
 
Anonymous



habe es noch nicht hingekriegt, habe aber der Hauptfehler gefunden.
die Matrixdaten würden in der DB auf 8 Stellen gerundet, was +- 5Km bedeutete.
Anonymous



danke für das umformatieren mitlerweile habe ich die DB angepasst
und die Lösung war dadurch einfach, ist diesmal aber in PHP nicht mehr in JS damit die Abfrage direkt auf dem serwer statfindet.

function dist($lat1, $lng1,$lat2,$lng2) {
$a1 = 2*pi()*$lat1/360;$b1 = 2*pi()*$lng1/360;
$a2 = 2*pi()*$lat2/360;$b2 = 2*pi()*$lng2/360;
$d = acos(cos($a1)*cos($b1)*cos($a2)*cos($b2)+cos($a1)*sin($b1)*cos($a2)*sin($b2)+sin($a1)*sin($a2));
$d1=$d*6372;
}

$lat1=$lat1; $lng1=$lng1 //koordinate Benützer generiert durch PLZ eingabe und dem php script übergeben
$kmd=$kmd //$kmd = maximale Distanzangabe vom benützer
$result1=mysql_query("SELECT * FROM ivents");
$tresult1=mysql_fetch_row($result1);
while($row = mysql_fetch_row($result1)){
$lng2=$row[11];
$lat2=$row[12];
$d1= dist($lat1, $lng1, $lat2, $lng2);
if($d1<=$kmd){
echo '<div id.......................';
}}
Anonymous



Ich würde die Abfrage in so einem Fall auch auf dem Server machen. Das heisst für jeden Event vorgängig (beim Insert) die Koordinaten bestimmen und bei der Suche die Koordinaten der gesuchten PLZ bestimmen und dann auf dem Server alle Events im gesuchten Abstand zu PLZ.

Eventuell könnte man aus Performancegründen die lat/lng auch in die Where-Bedingungen einbauen. Wenn der Datenbankhersteller die kreisförmige Suche nicht ermöglicht, kann man auch eine viereckige Suche auf der Datenbank verwenden und es danach noch wie oben in php auf eine kreisförmige Suche reduzieren.

Marc
 
Forum Index -> Forum in Deutsch
Go to:   
Powered by JForum 2.1.5 © JForum Team