구글맵 구글지도 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년 전 | 4402 | ||
| 2634 | 6개월 전 | 609 | ||
| 2633 | 6개월 전 | 598 | ||
| 2632 |
|
6개월 전 | 508 | |
| 2631 | 6개월 전 | 569 | ||
| 2630 |
세르반데스
|
6개월 전 | 538 | |
| 2629 | 6개월 전 | 727 | ||
| 2628 | 6개월 전 | 412 | ||
| 2627 | 6개월 전 | 417 | ||
| 2626 |
이슈DEV
|
7개월 전 | 636 | |
| 2625 |
welcome
|
7개월 전 | 645 | |
| 2624 |
이슈DEV
|
7개월 전 | 449 | |
| 2623 | 7개월 전 | 407 | ||
| 2622 | 7개월 전 | 496 | ||
| 2621 | 7개월 전 | 350 | ||
| 2620 |
|
7개월 전 | 369 | |
| 2619 | 7개월 전 | 479 | ||
| 2618 | 7개월 전 | 465 | ||
| 2617 | 7개월 전 | 554 | ||
| 2616 | 7개월 전 | 678 | ||
| 2615 | 7개월 전 | 581 | ||
| 2614 | 7개월 전 | 410 | ||
| 2613 |
바닐라코드
|
7개월 전 | 730 | |
| 2612 | 7개월 전 | 594 | ||
| 2611 | 8개월 전 | 731 | ||
| 2610 | 8개월 전 | 966 | ||
| 2609 | 8개월 전 | 508 | ||
| 2608 | 8개월 전 | 653 | ||
| 2607 | 8개월 전 | 630 | ||
| 2606 | 8개월 전 | 576 | ||
| 2605 | 8개월 전 | 602 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기