2012-05-19 5 views
2

나는 mysql 데이터베이스에 데이터가 있고 PHP 배열에서 해당 데이터를 읽고 google geolocation에 사용할 수 있기를 원합니다. 나는 몇 가지 매개 변수와 함께 당신을 귀찮게하지 않습니다으로 만 SELECT * 문을 사용하려면이 그래서 예를 들면 mysql에서 Google geolocation에 PHP 배열을 반환하는 방법

은 ...

내가 달성하고자하는 지점에서 B 지점으로 라인 (표시 얻을 수 있습니다 GPS 위치가 저장된 위치에 따라 다름)

이것은 내지도에 갖고 싶은 링크입니다. POLYLINE이 예에서는 배열에 4 개의 데이터 만 있고 싶습니다.

이제 코드로 돌아갈 수 있습니다. 이것은 mysql에 연결하기위한 PHP 스크립트이다. mysqli를 사용하여 나중에 데이터베이스에 프로 시저를 저장할 것이므로 혼동하지 마십시오. 구글 API 페이지의 예는 코드이 PHP 스크립트를 연결하고 버튼 대신 고정 사람의 클릭하면 내 배열 값을 삽입하는 방법

class dbMySql 
{ 
static function Exec($query) { 
// open database 
$conn = mysqli_connect(
$GLOBALS['cfg_db_Server'], 
$GLOBALS['cfg_db_User'], 
$GLOBALS['cfg_db_Pass'] 
); 
if($conn === false) 
{ 
    throw new Exception(mysqli_connect_error()); 
} 
mysqli_select_db($conn,$GLOBALS['cfg_db_Name']); 
$result = mysqli_query($conn,$query); 
if(is_bool($result) && !$result) 
{ 
    $error = mysqli_error($conn); 
    mysqli_close($conn); 
    throw new Exception($error); 
} 
mysqli_close($conn); 
return $result; 
} 
} 

:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <script type="text/javascript" 
     src="http://maps.googleapis.com/maps/api/js?key=KEY&sensor=true"> 
    </script> 
    <script type="text/javascript"> 
    function initialize() { 
    var myLatLng = new google.maps.LatLng(0, -180); 
    var myOptions = { 
    zoom: 3, 
    center: myLatLng, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 

    var map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 
    var flightPlanCoordinates = [ 
    new google.maps.LatLng(37.772323, -122.214897), 
    new google.maps.LatLng(21.291982, -157.821856), 
    new google.maps.LatLng(-18.142599, 178.431), 
    new google.maps.LatLng(-27.46758, 153.027892) 
    ]; 
    var flightPath = new google.maps.Polyline({ 
    path: flightPlanCoordinates, 
    strokeColor: "#FF0000", 
    strokeOpacity: 1.0, 
    strokeWeight: 2 
    }); 

    flightPath.setMap(map); 
} 
    </script> 
    </head> 
    <body onload="initialize()"> 
    <div id="map_canvas" style="width:100%; height:80%"></div> 
    <div><button type="button">Click Me!</button></div> 
    </body> 
</html> 

편집 :

error

EDIT2 :

이것은 내가 코드를 할 때 내가 무엇을 얻을3210

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <style type="text/css"> 
     html { height: 100% } 
     body { height: 100%; margin: 0; padding: 0 } 
     #map_canvas { height: 100% } 
    </style> 
    <script type="text/javascript" 
     src="http://maps.googleapis.com/maps/api/js?key=key&sensor=true"> 
    </script> 
    <script type="text/javascript"> 
    function initialize() { 
    var myLatLng = new google.maps.LatLng(0, 180); 
    var myOptions = { 
    zoom: 3, 
    center: myLatLng, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 

    var map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 
    var flightPlanCoordinates = [ <?php echo implode(',', $coordinates) ?> ]; 
    ]; 
    var flightPath = new google.maps.Polyline({ 
    path: flightPlanCoordinates, 
    strokeColor: "#FF0000", 
    strokeOpacity: 1.0, 
    strokeWeight: 2 
    }); 

    flightPath.setMap(map); 
} 
</script> 
</script> 
    <?PHP 
class dbMySql { 

    static function Exec($query) { 
     // open database 
     $conn = mysqli_connect('localhost','root','*****'); 
     if($conn === false) { 
      throw new Exception(mysqli_connect_error()); 
    } 
     mysqli_select_db($conn,'data_gps'); 

    $result = mysqli_query($conn,$query); 

     if(is_bool($result) && !$result) { 
      $error = mysqli_error($conn); 
      mysqli_close($conn); 
      throw new Exception($error); 
     } 

     mysqli_close($conn); 

    return $result; 
    } 
} 
$coordinates = array(); 
$result = dbMySql::Exec('SELECT lat,lng FROM data'); 
while ($row = mysqli_fetch_assoc($result)) 
    $coordinates[] = 'new google.maps.LatLng(' . $row['lat'] . ', ' . $row['lng'] . ')' ; 
?> 
    </script> 
    </head> 
    <body onload="initialize()"> 
    <div id="map_canvas" style="width:100%; height:80%"></div> 
    <div><button type="button">Click Me!</button></div> 
    </body> 
</html> 

답변

2

당신은 결과 사이를 반복하고 자바 스크립트에 반향시켜야한다고 생각합니다.

당신은 위도와 경도가 데이터베이스에 저장되어 있다고 가정합니다.

$coordinates = array(); 
$result = dbMySql::Exec('SELECT lat, lng FROM table WHERE id = 1'); 
while ($row = mysqli_fetch_assoc($result)) 
    $coordinates[] = 'new google.maps.LatLng(' . $row['lat'] . ', ' . $row['lng'] . ')'; 

그리고

그러면 자바 스크립트 섹션에

var flightPlanCoordinates = [ <?php echo implode(',', $coordinates) ?> ]; 

편집 :

<!DOCTYPE html> 
<?PHP 
class dbMySql { 

    static function Exec($query) { 
     // open database 
     $conn = mysqli_connect('localhost','root','*****'); 
     if($conn === false) { 
      throw new Exception(mysqli_connect_error()); 
    } 
     mysqli_select_db($conn,'data_gps'); 

    $result = mysqli_query($conn,$query); 

     if(is_bool($result) && !$result) { 
      $error = mysqli_error($conn); 
      mysqli_close($conn); 
      throw new Exception($error); 
     } 

     //mysqli_close($conn); 

    return $result; 
    } 
} 
$coordinates = array(); 
$result = dbMySql::Exec('SELECT lat,lng FROM data'); 
while ($row = mysqli_fetch_assoc($result)) 
    $coordinates[] = 'new google.maps.LatLng(' . $row['lat'] . ', ' . $row['lng'] . ')'; 
?> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <style type="text/css"> 
     html { height: 100% } 
     body { height: 100%; margin: 0; padding: 0 } 
     #map_canvas { height: 100% } 
    </style> 
    <script type="text/javascript" 
     src="http://maps.googleapis.com/maps/api/js?key=key&sensor=true"> 
    </script> 
    <script type="text/javascript"> 
    function initialize() { 
    var myLatLng = new google.maps.LatLng(0, 180); 
    var myOptions = { 
    zoom: 3, 
    center: myLatLng, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 

    var map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 
    var flightPlanCoordinates = [<?php echo implode(',', $coordinates) ?>]; 
    var flightPath = new google.maps.Polyline({ 
    path: flightPlanCoordinates, 
    strokeColor: "#FF0000", 
    strokeOpacity: 1.0, 
    strokeWeight: 2 
    }); 

    flightPath.setMap(map); 
} 
</script> 
    </head> 
    <body onload="initialize()"> 
    <div id="map_canvas" style="width:100%; height:80%"></div> 
    <div><button type="button">Click Me!</button></div> 
    </body> 
</html> 
+0

안녕하세요, 난 당신의 코드를 시도했습니다. 에코 좌표를 수행 할 때 나는 경도와 위도의 결과를 실제로 얻고 있습니다. 하지만 코드에 넣으면 작동하지 않습니다. 지도가로드되지 않습니다. 스크립트 안에 PHP를 넣었습니다. –

+0

위 그림의 오류를 넣었습니다. –

+0

파일이 '.php'로 끝나나요? – mpratt

관련 문제