구글맵 구글지도 api db에 주소가져와서 지도 생성하기
db에 저장된 주소를 가져와서 구글지도로 보여주기 위한 방법입니다.
예전방식으로 잘 안되는 것이 있어 수정해서 올립니다.
<?
//구글API를 사용 하기 위해선 먼저 구글
//https://cloud.google.com/maps-platform/?hl=ko 접속하여 필요한 api를 선택해야 합니다. Geocoding => 이 api를 선택해야 좌표를 정상적으로 가져옵니다.
$address =$view['wr_5']; //db에 저장된 주소
//주소를 구글로 보내 해당 주소 데이타를 가져옵니다.
function getGeoInfo_GoogleMap($address){
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&key=구글KEY값넣어주세요';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if ($result === FALSE) {
error_log('Curl failed');
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
//주소를 넘겨주고 데이타를 가져옵니다.
$addr_info =getGeoInfo_GoogleMap($address);
//json데이터를 decode하여 사용합니다.
$addr_temp = json_decode($addr_info,true);
//필요한 좌표값만 배열에서 추출하여 사용합니다. 이전배열하고 달라졌습니다.
$addr_position = $addr_temp['results'][0]['geometry']['location'];
//필요한 좌표값을 변수에 담습니다.
$lat =$addr_position['lat'];
$lng =$addr_position['lng'];
?>
<!-- 구글API를 선언합니다. initialize 함수를 바로 호출합니다. -->
<script src="https://maps.googleapis.com/maps/api/js?key=구글KEY값넣어주세요=initialize" async defer></script>
<scirpt>
function initialize() {
var myLatlng = new google.maps.LatLng(<?=$lat?>, <?=$lng?>); // 좌표값
var mapOptions = {
zoom: 16, // 지도 확대레벨 조정
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "<?=$view['wr_subject']?>" // 마커에 마우스를 올렸을때 간략하게 표기될 설명글
});
}
</script>
댓글 10개
도전해봐야겠네요 ^^
게시판 목록
그누보드5 팁자료실
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 공지 | 3년 전 | 4427 | ||
| 144 |
Lemios
|
11년 전 | 6683 | |
| 143 | 11년 전 | 13452 | ||
| 142 |
|
11년 전 | 6102 | |
| 141 | 11년 전 | 5447 | ||
| 140 | 11년 전 | 6477 | ||
| 139 | 11년 전 | 6582 | ||
| 138 | 11년 전 | 12134 | ||
| 137 | 11년 전 | 9989 | ||
| 136 | 11년 전 | 7131 | ||
| 135 |
skaxoddl
|
11년 전 | 9794 | |
| 134 | 11년 전 | 7514 | ||
| 133 | 11년 전 | 7356 | ||
| 132 | 11년 전 | 5417 | ||
| 131 | 11년 전 | 9683 | ||
| 130 | 11년 전 | 5256 | ||
| 129 | 11년 전 | 14681 | ||
| 128 | 11년 전 | 11967 | ||
| 127 | 11년 전 | 9746 | ||
| 126 | 11년 전 | 9708 | ||
| 125 | 11년 전 | 6270 | ||
| 124 | 11년 전 | 7686 | ||
| 123 | 11년 전 | 5752 | ||
| 122 | 11년 전 | 5556 | ||
| 121 | 12년 전 | 8459 | ||
| 120 | 12년 전 | 7779 | ||
| 119 | 12년 전 | 11893 | ||
| 118 | 12년 전 | 9397 | ||
| 117 | 12년 전 | 7707 | ||
| 116 | 12년 전 | 6709 | ||
| 115 |
|
12년 전 | 6830 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기