구글맵 구글지도 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년 전 | 4406 | ||
| 2544 | 11개월 전 | 942 | ||
| 2543 | 11개월 전 | 842 | ||
| 2542 | 11개월 전 | 745 | ||
| 2541 | 11개월 전 | 774 | ||
| 2540 | 12개월 전 | 1185 | ||
| 2539 | 12개월 전 | 1020 | ||
| 2538 | 12개월 전 | 1003 | ||
| 2537 |
|
12개월 전 | 841 | |
| 2536 | 1년 전 | 1227 | ||
| 2535 |
|
1년 전 | 779 | |
| 2534 |
|
1년 전 | 887 | |
| 2533 |
거북이와두루미
|
1년 전 | 1661 | |
| 2532 | 1년 전 | 794 | ||
| 2531 | 1년 전 | 674 | ||
| 2530 | 1년 전 | 720 | ||
| 2529 | 1년 전 | 1062 | ||
| 2528 |
|
1년 전 | 554 | |
| 2527 | 1년 전 | 1132 | ||
| 2526 | 1년 전 | 744 | ||
| 2525 | 1년 전 | 1107 | ||
| 2524 | 1년 전 | 912 | ||
| 2523 | 1년 전 | 1334 | ||
| 2522 | 1년 전 | 957 | ||
| 2521 | 1년 전 | 1309 | ||
| 2520 | 1년 전 | 1185 | ||
| 2519 | 1년 전 | 1059 | ||
| 2518 |
Lemios
|
1년 전 | 866 | |
| 2517 | 1년 전 | 955 | ||
| 2516 | 1년 전 | 1702 | ||
| 2515 | 1년 전 | 845 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기