แสดง google map แบบ popup message ด้วย jquery

เขียนเมื่อ 11 ปีก่อน โดย Ninenik Narkdee
google map jquery popup

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ google map jquery popup

ดูแล้ว 10,398 ครั้ง


เนื้อหานี้ เป็นการดัดแปลง และประยุกต์จากเนื้อหา 2 บทความด้านล่าง
 
สร้าง popup message ด้วย jquery อย่างง่าย 
 
แสดงแผนที่ 2 แผนที่ ด้วย google map ใน jqueryui tab 
 
รายละเอียด อ่านได้ในแต่ละบนความ ในที่นี้ แค่เป็นการ นำ jqueryui tab ที่มีแผนที่
อยู่แล้ว ไปไว้ในส่วนของ popup message 
 
ตัวอย่าง

test test
test test
test test
test test
คลิกที่นี้ เพื่อแสดงแผนที่ แบบ popup
test test
test test
test test
test test
test test
test test
X

รายละเอียดย่อยหัวข้อแท็บที่ 1

Latitude
Longitude
Zoom

 
โค้ดทั้งหมด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<?php   
$jquery_ui_v="1.8.5";   
$theme=array(   
    "0"=>"base",   
    "1"=>"black-tie",   
    "2"=>"blitzer",   
    "3"=>"cupertino",   
    "4"=>"dark-hive",   
    "5"=>"dot-luv",   
    "6"=>"eggplant",   
    "7"=>"excite-bike",   
    "8"=>"flick",   
    "9"=>"hot-sneaks",   
    "10"=>"humanity",   
    "11"=>"le-frog",   
    "12"=>"mint-choc",   
    "13"=>"overcast",   
    "14"=>"pepper-grinder",   
    "15"=>"redmond",   
    "16"=>"smoothness",   
    "17"=>"south-street",   
    "18"=>"start",   
    "19"=>"sunny",   
    "20"=>"swanky-purse",   
    "21"=>"trontastic",   
    "22"=>"ui-darkness",   
    "23"=>"ui-lightness",   
    "24"=>"vader"   
);   
$jquery_ui_theme=$theme[14];   
?>   
<link type="text/css" rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/<?=$jquery_ui_v?>/themes/<?=$jquery_ui_theme?>/jquery-ui.css" />   
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/<?=$jquery_ui_v?>/jquery-ui.min.js"></script>
<style type="text/css">   
html,body{ 
    padding:0px; 
    margin:0px; 
    height: 100%;      
body{
    font-family:Tahoma, Geneva, sans-serif;
    font-size:12px;
    width: 100%;       
}
div.test_demo_main{
    margin:auto;
    width:75%; 
}
/* ปรับขนาดตัวอักษรของข้อความใน tabs  
สามารถปรับเปลี่ยน รายละเอียดอื่นๆ เพิ่มเติมเกี่ยวกับ tabs 
*/   
.ui-tabs{   
    font-family:tahoma;   
    font-size:11px;   
}   
 
div#map_canvas{ 
    margin:auto; 
    width:550px; 
    height:350px; 
    overflow:hidden; 
div#map_canvas2{ 
    margin:auto; 
    width:550px; 
    height:350px; 
    overflow:hidden; 
#overlay {    
    background:#000; 
    width:100%; 
    height:100%; 
    z-index:80000; 
    top:0px; 
    left:0px; 
    position:fixed; 
    opacity: .5;    
    filter: alpha(opacity=50);    
    -moz-opacity: .5;   
    display:none; 
}    
.msg_show{ 
    position:fixed; 
    z-index:90000; 
    margin:auto; 
    width:600px; 
    height:550px; 
    top: 20%; 
    left: 50%; 
    margin-top: -100px; 
    margin-left: -250px; 
    text-align:center; 
    display:none; 
.msg_data{ 
    margin-top: 10px;
    margin-right: 10px;
}
.close_popup{
    color: #FFF;
    position: absolute;
    display: block;
    width: 25px;
    height: 25px;
    background-color: #000;
    z-index: 80001;
    text-decoration: none;
    border: 2px solid #FFF;
    -webkit-border-radius: 100%;
    -moz-border-radius: 100%;
    border-radius: 100%;
    line-height: 23px;
    font-weight: bold;
    font-size: 16px;
    right: 0px;
}
</style>   
 
</head>
 
<body>
 
 
 
 
<div class="test_demo_main">
 
 
<div style="margin:auto;width:95%;"
test test<br />test test<br /> 
test test<br />test test<br /> 
<a class="show_box" href="javascript:void(0);">คลิกที่นี้ เพื่อแสดงแผนที่ แบบ popup</a> 
<!--เนื้อหาภายในเว็บ-->  <br /> 
test test<br />test test<br /> 
test test<br />test test<br /> 
test test<br />test test<br /> 
</div> 
 
 
<div class="msg_show"
<a class="show_box close_popup" href="javascript:void(0);">X</a> 
<div class="msg_data"
 
 
<div id="tabs"
    <ul> 
        <li><a href="#tabs-1">หัวข้อแท็บที่ 1</a></li> 
        <li><a href="#tabs-2">หัวข้อแท็บที่ 2</a></li> 
        <li><a href="#tabs-3">หัวข้อแท็บที่ 3</a></li> 
    </ul> 
    <div id="tabs-1"
        <p>รายละเอียดย่อยหัวข้อแท็บที่ 1</p> 
    </div> 
    <div id="tabs-2"
        <p>
         
        <div id="map_canvas2"></div> 
        <div id="showDD" style="margin:auto;padding-top:5px;width:550px;">   
        <form id="form_get_detailMap" name="form_get_detailMap" method="post" action="">   
        Latitude   
        <input name="lat_value" type="text" id="lat_value" value="0" />  <br /> 
        Longitude   
        <input name="lon_value" type="text" id="lon_value" value="0" />  <br /> 
        Zoom   
        <input name="zoom_value" type="text" id="zoom_value" value="0" size="5" />   
        <br /> 
        <input type="submit" name="button" id="button" value="บันทึก" />   
        </form>   
        </div>        
         
        </p> 
    </div> 
    <div id="tabs-3"
        <p>
         
  <div id="map_canvas"></div> 
 
      
         
        </p> 
    </div> 
</div> 
 
 
</div> 
</div> 
 
   
 
 
</div>
 
 
<script type="text/javascript"
$(function(){ 
 
    $(".show_box").click(function(){ 
        $("#overlay").fadeToggle("",function(){ // แสดงส่วนของ overlay 
            $(".msg_show").slideToggle("",function(){ // แสดงส่วนของ เนื้อหา popup 
                if($(this).css("display")=="block"){        // ถ้าเป็นกรณีแสดงข้อมูล  
                initialize();
                //  หากต้องการดึงข้อมูลมาแสดง แบบ ajax 
                //  สามารถดัดแปลงจากโค้ดนี้ได้       
                //  $(".msg_data").load("data.php");     
                //      หรือ 
                //  $.post("data.php",{},function(data){ 
                //      $(".msg_data").html(data); 
                //  }); 
                
            }); 
        }); 
    }); 
     
 
    // แทรกโค้ต jquery 
    // กำหนดแบบทั่วไป 
    $("#tabs").tabs();  
    $("a[href='#tabs-2']").click(function(){ 
        initialize2(); 
    });    
    $("a[href='#tabs-3']").click(function(){ 
        initialize(); 
    });            
 
}); 
</script> 
<script type="text/javascript"
var map; // กำหนดตัวแปร map ไว้ด้านนอกฟังก์ชัน เพื่อให้สามารถเรียกใช้งาน จากส่วนอื่นได้ 
var GGM; // กำหนดตัวแปร GGM ไว้เก็บ google.maps Object จะได้เรียกใช้งานได้ง่ายขึ้น 
// ฟังก์ชันสำหรับสร้างแผนที่ ใน tab ที่ 1 ใช้ชื่อว่า initialize()
function initialize() { // ฟังก์ชันแสดงแผนที่ 
    GGM=new Object(google.maps); // เก็บตัวแปร google.maps Object ไว้ในตัวแปร GGM 
    // กำหนดจุดเริ่มต้นของแผนที่ 
    var my_Latlng  = new GGM.LatLng(13.761728449950002,100.6527900695800); 
    var my_mapTypeId=GGM.MapTypeId.ROADMAP; // กำหนดรูปแบบแผนที่ที่แสดง 
    // กำหนด DOM object ที่จะเอาแผนที่ไปแสดง ที่นี้คือ div id=map_canvas 
    var my_DivObj=$("#map_canvas")[0];  
    // กำหนด Option ของแผนที่ 
    var myOptions = { 
        zoom: 13, // กำหนดขนาดการ zoom 
        center: my_Latlng , // กำหนดจุดกึ่งกลาง 
        mapTypeId:my_mapTypeId // กำหนดรูปแบบแผนที่ 
    }; 
    map = new GGM.Map(my_DivObj,myOptions);// สร้างแผนที่และเก็บตัวแปรไว้ในชื่อ map 
   
   
   
    // เรียกใช้คุณสมบัติ ระบุตำแหน่ง ของ html 5 ถ้ามี 
    if(navigator.geolocation){ 
            navigator.geolocation.getCurrentPosition(function(position){ 
                var pos = new GGM.LatLng(position.coords.latitude,position.coords.longitude); 
                var infowindow = new GGM.InfoWindow({ 
                    map: map, 
                    position: pos, 
                    content: "คุณอยู่ที่นี่." 
                }); 
                   
                var my_Point = infowindow.getPosition();  // หาตำแหน่งของตัว marker เมื่อกดลากแล้วปล่อย 
                map.panTo(my_Point);  // ให้แผนที่แสดงไปที่ตัว marker             
                map.setCenter(pos); 
            },function() { 
                // คำสั่งทำงาน ถ้า ระบบระบุตำแหน่ง geolocation ผิดพลาด หรือไม่ทำงาน 
            }); 
    }else
         // คำสั่งทำงาน ถ้า บราวเซอร์ ไม่สนับสนุน ระบุตำแหน่ง 
    
 
 
// ฟังก์ชันสำหรับสร้างแผนที่ ใน tab ที่ 2 ใช้ชื่อว่า initialize2()
function initialize2() { // ฟังก์ชันแสดงแผนที่ 
    GGM=new Object(google.maps); // เก็บตัวแปร google.maps Object ไว้ในตัวแปร GGM 
    // กำหนดจุดเริ่มต้นของแผนที่ 
    var my_Latlng  = new GGM.LatLng(13.761728449950002,100.6527900695800); 
    var my_mapTypeId=GGM.MapTypeId.ROADMAP; // กำหนดรูปแบบแผนที่ที่แสดง 
    // กำหนด DOM object ที่จะเอาแผนที่ไปแสดง ที่นี้คือ div id=map_canvas 
    var my_DivObj=$("#map_canvas2")[0];  
    // กำหนด Option ของแผนที่ 
    var myOptions = { 
        zoom: 13, // กำหนดขนาดการ zoom 
        center: my_Latlng , // กำหนดจุดกึ่งกลาง 
        mapTypeId:my_mapTypeId // กำหนดรูปแบบแผนที่ 
    }; 
    map = new GGM.Map(my_DivObj,myOptions);// สร้างแผนที่และเก็บตัวแปรไว้ในชื่อ map 
       
    var my_Marker = new GGM.Marker({ // สร้างตัว marker 
        position: my_Latlng,  // กำหนดไว้ที่เดียวกับจุดกึ่งกลาง 
        map: map, // กำหนดว่า marker นี้ใช้กับแผนที่ชื่อ instance ว่า map 
        draggable:true, // กำหนดให้สามารถลากตัว marker นี้ได้ 
        title:"คลิกลากเพื่อหาตำแหน่งจุดที่ต้องการ!" // แสดง title เมื่อเอาเมาส์มาอยู่เหนือ 
    }); 
       
    // กำหนด event ให้กับตัว marker เมื่อสิ้นสุดการลากตัว marker ให้ทำงานอะไร 
    GGM.event.addListener(my_Marker, "dragend", function() { 
        var my_Point = my_Marker.getPosition();  // หาตำแหน่งของตัว marker เมื่อกดลากแล้วปล่อย 
        map.panTo(my_Point);  // ให้แผนที่แสดงไปที่ตัว marker        
        $("#lat_value").val(my_Point.lat());  // เอาค่า latitude ตัว marker แสดงใน textbox id=lat_value 
        $("#lon_value").val(my_Point.lng()); // เอาค่า longitude ตัว marker แสดงใน textbox id=lon_value  
        $("#zoom_value").val(map.getZoom()); // เอาขนาด zoom ของแผนที่แสดงใน textbox id=zoom_value 
    });      
   
    // กำหนด event ให้กับตัวแผนที่ เมื่อมีการเปลี่ยนแปลงการ zoom 
    GGM.event.addListener(map, "zoom_changed", function() { 
        $("#zoom_value").val(map.getZoom()); // เอาขนาด zoom ของแผนที่แสดงใน textbox id=zoom_value   
    }); 
   
$(function(){ 
    // โหลด สคริป google map api เมื่อเว็บโหลดเรียบร้อยแล้ว 
    // ค่าตัวแปร ที่ส่งไปในไฟล์ google map api 
    // v=3.2&sensor=false&language=th&callback=initialize 
    //  v เวอร์ชัน่ 3.2 
    //  sensor กำหนดให้สามารถแสดงตำแหน่งทำเปิดแผนที่อยู่ได้ เหมาะสำหรับมือถือ ปกติใช้ false 
    //  language ภาษา th ,en เป็นต้น 
    //  callback ให้เรียกใช้ฟังก์ชันแสดง แผนที่ initialize 
    $("<script/>", { 
      "type": "text/javascript"
      src: "//maps.google.com/maps/api/js?v=3.2&sensor=false&language=th&callback=initialize" 
    }).appendTo("body");     
}); 
</script>   
<div id="overlay"></div>  
</body>
</html>
 
 


กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ











URL สำหรับอ้างอิง










เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ