发送经纬度值MySQL查询更新通过routeboxer谷歌地图标记经纬度、标记、地图、routeboxer

2023-09-10 14:54:41 作者:约你望风

我用Routerboxer生成的纬度和经度的某条路在列表中。我想lat和LNGS发回给我的数据库查询(用ajax我presume)的范围内返回的标记。

我已经搜查,发现该数据库查询必须是:

SELECT * FROM as24 WHERE纬度>一个和LAT<体c,LNG> b和LNG< ð

我的问题是我怎么把这些A,B,C和D的到PHP页面?

 函数calcRoute(){
clearBoxes();
距离= 10 * 1.609344; //内10英里
VAR开始=的document.getElementById('开始')值。
VAR结束=的document.getElementById(结束)的价值。
VAR请求= {
产地:启动,
目的地:最后,
travelMode:google.maps.TravelMode.DRIVING
};
directionService.route(要求,功能(结果,状态){
    如果(状态== google.maps.DirectionsStatus.OK){
      directionsRenderer.setDirections(结果);

      围绕第一路由的概述路径//框
      VAR路径= result.routes [0] .overview_path;
      VAR盒= routeBoxer.box(路径,距离);
      drawBoxes(箱);
    } 其他 {
      警报(路线查询失败:+状态);
    }
  });
}



//画出框的阵列在地图上折线
功能drawBoxes(盒){
  boxpolys =新阵列(boxes.length);
  对于(VAR I = 0; I< boxes.length;我++){
    boxpolys [我] =新google.maps.Rectangle({
      范围:盒[我],
      fillOpacity:0,
      strokeOpacity:1.0,
      则strokeColor:#000000,
      strokeWeight:1,
      地图:地图
      //执行搜索
    });
  }
}

//清除盒当前在地图上
功能clearBoxes(){
  如果(boxpolys!= NULL){
    对于(VAR I = 0; I< boxpolys.length;我++){
      boxpolys [I] .setMap(空);
    }
  }
  boxpolys = NULL;
}


google.maps.event.addDomListener(窗口,负荷初始化);

功能负载(){

VAR信息窗口=新google.maps.InfoWindow;

//改变这取决于你的PHP文件的名称
downloadUrl(as24_genxml.php功能(数据){
VAR XML = data.responseXML;
VAR的标记= xml.documentElement.getElementsByTagName(标记);
对于(VAR I = 0; I< markers.length;我++){
  VAR名称=标记[I] .getAttribute(姓名);
  VAR地址=标记[I] .getAttribute(地址);
  VAR价格=标记[I] .getAttribute(代价);
  VAR类型=标志物[I] .getAttribute(类);
  VAR点=新google.maps.LatLng(
      parseFloat(标记[I] .getAttribute(土地增值税)),
      parseFloat(标记物[I] .getAttribute(LNG)));
  VAR HTML =< B>中+姓名++价格+< / B>< BR />中+地址;
  VAR图标= customIcons [类型] || {};
  VAR的标记=新google.maps.Marker({
    地图:地图,
    位置:点,
    图标:icon.icon
  });
  map.getBounds()。包括(marker.getPosition())
  bindInfoWindow(标记,地图,信息窗口,HTML);
  $。员额(as24_genxml.php
{
  答:map.getBounds()getNorthEast()纬度()
  B:map.getBounds()getNorthEast()液化天然气(),
  C:map.getBounds()getSouthWest()纬度()
  D:map.getBounds()getSouthWest()液化天然气()。
},
 功能(数据,状态){

});


}
});
}
 

正如你可以看到我已经尝试过通过Ajax的PHP发送做到这一点。

我的PHP文件看起来是这样的:

 < PHP的包括:(的PHP / config.php文件);

功能parseToXML($ htmlStr)
{
$ xmlStr = str_replace函数('<','&放大器; LT;',$ htmlStr);
$ xmlStr = str_replace函数('>','&放大器; GT;',$ xmlStr);
$ xmlStr = str_replace函数('','和; QUOT;',$ xmlStr);
$ xmlStr = str_replace函数(','和;#39;',$ xmlStr);
$ xmlStr = str_replace函数(&放大器;,&放大器;放大器;',$ xmlStr);
返回$ xmlStr;
}

//打开一个到MySQL服务器的连接
$连接=的mysql_connect($ mysql_server,$ mysql_user,$ mysql_pass);
如果(!$连接){
死亡(未连接:mysql_error());
}

//设置活动的MySQL数据库
$ db_selected = mysql_select_db($ mysql_db,$连接);
如果(!$ db_selected){
死亡(可以\'吨使用DB:mysql_error());
}

//选择在标记表中的所有行
$查询=SELECT * FROM as24 WHERE纬度>在和LAT c为C和液化天然气> b和LNG< D​​;
$结果= mysql_query($查询);
如果(!$结果){
模具(无效的问题:mysql_error());
}

标题(内容类型:文本/ XML);

//开始XML文件,回声父节点
回声'<标记>';

//遍历对每个行,打印XML节点
而($行= @mysql_fetch_assoc($结果)){
//增加来XML文档节点
回声'<标记;
回声NAME ='parseToXML($行['名称'])。'';
回声地址='parseToXML($行['地址'])。'';
回声价格='parseToXML($行['价格'])。'';
回声'叻='$行['纬度']。'';
回声'LNG ='$行['LNG']。'';
回声类型='$行['类型']。'';
回声/>';
}

//结束XML文件
回声'< /标记>';

?>
 

解决方案 如何在GOOGLE地图上加上新的地方位置

下面是如何使用的范围发送到你的PHP code为例 jQuery.ajax()

  VAR边界= map.getBounds();
VAR西南= bounds.getSouthWest();
VAR东北= bounds.getNorthEast();

$阿贾克斯({

    网址:your_file.php,
    缓存:假的,
    数据: {
        'fromlat':southWest.lat(),
        'tolat':northEast.lat(),
        'fromlng':southWest.lng(),
        tolng:northEast.lng()
    },
    数据类型:JSON,
    异步:假的,
    成功:功能(数据){

        $每个(数据,功能(我,标记){

            createMarker(标记);
        });
    }
});

函数createMarker(数据){

     //检查你有什么在这里数据和构建你的标志,从这里,你会做任何标记。
}
 

在你的PHP:

  $ fromlat = $ _GET ['fromlat'];
$ tolat = $ _GET ['tolat'];
$ fromlng = $ _GET ['fromlng'];
$ tolng = $ _GET ['tolng'];

$结果=阵列();

$ SQL =SELECT * FROM your_table那里(LAT>'。$ DB-> real_escape_string($ fromlat)'和北纬<'。$ DB-> real_escape_string($ tolat)。和LNG> '$ DB-> real_escape_string($ fromlng)'和液化天然气&所述;'。$ DB-> real_escape_string($ tolng)');

$结果= $ DB->查询($ SQL)或死亡($ DB->错误);

而($ OBJ = $ result-> fetch_object()){

    $结果[] = $ OBJ;
}

回声json_en code($结果);
 

当然,如果你喜欢的POST而不是GET,你能适应。

I'm using Routerboxer to generate a list of latitudes and longitudes within a certain route. I want to send the lat and lngs back to my database query (I presume using ajax) to return markers within the bounds.

I have searched and found the database query needs to be:

SELECT * FROM as24 WHERE lat > a AND lat < c AND lng > b AND lng < d

My question is how do I send these a,b,c and d's to the PHP page?

function calcRoute() {
clearBoxes();
distance = 10 * 1.609344; //within 10 miles
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsRenderer.setDirections(result);

      // Box around the overview path of the first route
      var path = result.routes[0].overview_path;
      var boxes = routeBoxer.box(path, distance);
      drawBoxes(boxes);
    } else {
      alert("Directions query failed: " + status);
    }
  });
}



// Draw the array of boxes as polylines on the map
function drawBoxes(boxes) {
  boxpolys = new Array(boxes.length);
  for (var i = 0; i < boxes.length; i++) {
    boxpolys[i] = new google.maps.Rectangle({
      bounds: boxes[i],
      fillOpacity: 0,
      strokeOpacity: 1.0,
      strokeColor: '#000000',
      strokeWeight: 1,
      map: map
      //Perform Search
    });
  }
}

// Clear boxes currently on the map
function clearBoxes() {
  if (boxpolys != null) {
    for (var i = 0; i < boxpolys.length; i++) {
      boxpolys[i].setMap(null);
    }
  }
  boxpolys = null;
}


google.maps.event.addDomListener(window, "load", initialize);

function load() {

var infoWindow = new google.maps.InfoWindow;

// Change this depending on the name of your PHP file
downloadUrl("as24_genxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
  var name = markers[i].getAttribute("name");
  var address = markers[i].getAttribute("address");
  var price = markers[i].getAttribute("price");
  var type = markers[i].getAttribute("type");
  var point = new google.maps.LatLng(
      parseFloat(markers[i].getAttribute("lat")),
      parseFloat(markers[i].getAttribute("lng")));
  var html = "<b>" + name + " " + price + "</b> <br/>" + address;
  var icon = customIcons[type] || {}; 
  var marker = new google.maps.Marker({
    map: map,
    position: point,
    icon: icon.icon
  });
  map.getBounds().contains(marker.getPosition())
  bindInfoWindow(marker, map, infoWindow, html);
  $.post("as24_genxml.php",
{
  a:map.getBounds().getNorthEast().lat(),
  b:map.getBounds().getNorthEast().lng(),
  c:map.getBounds().getSouthWest().lat(),
  d:map.getBounds().getSouthWest().lng()
},
 function(data,status){

});


}
});
}

As you can see I've tried to do this via an ajax php send.

My php file looks like this:

<?php include ('php/config.php');

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 

// Opens a connection to a MySQL server
$connection=mysql_connect ($mysql_server, $mysql_user, $mysql_pass);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($mysql_db, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM as24 WHERE lat > a AND lat < c AND lng > b AND lng < d";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}

// End XML file
echo '</markers>';

?>

解决方案

Here is an example of how to send the bounds to your PHP code using jQuery.ajax().

var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();

$.ajax({

    url: 'your_file.php',
    cache: false,
    data: {
        'fromlat': southWest.lat(),
        'tolat': northEast.lat(),
        'fromlng': southWest.lng(),
        'tolng': northEast.lng()
    },
    dataType: 'json',
    async: false,
    success : function(data) {

        $.each(data, function(i,marker) {

            createMarker(marker);
        });
    }
});

function createMarker(data) {

     // Check what you have here in "data" and construct your markers from here as you would do for any marker.
}

In your PHP:

$fromlat = $_GET['fromlat'];
$tolat = $_GET['tolat'];
$fromlng = $_GET['fromlng'];
$tolng = $_GET['tolng'];

$results = array();

$sql = "SELECT * from your_table where (lat>'" . $db->real_escape_string($fromlat) . "' and lat<'" . $db->real_escape_string($tolat) . "' and lng>'" . $db->real_escape_string($fromlng) . "' and lng<'" . $db->real_escape_string($tolng) . "')";

$result = $db->query($sql) or die($db->error);

while ($obj = $result->fetch_object()) {

    $results[] = $obj;
}

echo json_encode($results);

Of course if you like POST instead of GET, you can adapt.