Google Store Location adalah layanan pointing lokasi tempat bisnis yang seringkali digunakan oleh Developer aplikasi untuk menyematkan po...
Google Store Location adalah layanan pointing lokasi tempat bisnis yang seringkali digunakan oleh Developer aplikasi untuk menyematkan posisi letak tempat bisnis ke dalam google map. Aplikasi map ini dapat menampilkan fitur nearby location atau find location store.
Saya akan berbagi tutorial basic programer menggunakan PHP 7 dan MySQL untuk menampilkan data lokasi bisnis yang sudah tersimpan kedalam database. Sebelum mengikuti tutorial berikut ada baiknya anda mendaftarkan diri terlebih dahulu untuk mendapatkan google map API Key.
Langkah pertama yang perlu dilakukan adalah :
- Buatlah file ke dalam sebuah folder utama config.php
1 2 3 4 5 | <?php $username="root"; $password=""; $database="test"; ?> |
- Berikutnya buatlah fungsi XML untuk menampilkan databse lokasi map dengan nama file map.php
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 | <?php require("config.php"); function parseToXML($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr); $xmlStr=str_replace('"','"',$xmlStr); $xmlStr=str_replace("'",''',$xmlStr); $xmlStr=str_replace("&",'&',$xmlStr); return $xmlStr; } // 1. Create a database connection $connection = mysqli_connect('localhost', $username, $password, $database); if (!$connection) { die("Database connection failed: " . mysqli_connect_error()); } $result = mysqli_query($connection,"SELECT * FROM markers"); if (!$result) { die('Invalid query: ' . mysql_error()); } header("Content-type: text/xml"); // Start XML file, echo parent node echo '<markers>'; // Iterate through the rows, printing XML nodes for each while ($row = mysqli_fetch_assoc($result)){ // Add to XML document node echo '<marker '; echo 'name="' . parseToXML($row['name']) . '" '; echo 'address="' . parseToXML($row['address']) . '" '; echo 'lat="' . $row['lat'] . '" '; echo 'lng="' . $row['lng'] . '" '; echo 'type="' . $row['type'] . '" '; echo '/>'; } // End XML file echo '</markers>'; ?> |
- Setelah proses diatas selesai dikerjakan, buatlah database store location dengan script dibawah
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 | -- phpMyAdmin SQL Dump -- version 4.8.0 -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 -- Generation Time: May 06, 2018 at 08:46 AM -- Server version: 10.1.31-MariaDB -- PHP Version: 7.2.4 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `test` -- -- -------------------------------------------------------- -- -- Table structure for table `markers` -- CREATE TABLE `markers` ( `id` int(11) NOT NULL, `name` varchar(60) NOT NULL, `address` varchar(80) NOT NULL, `lat` float(10,6) NOT NULL, `lng` float(10,6) NOT NULL, `type` varchar(30) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table `markers` -- INSERT INTO `markers` (`id`, `name`, `address`, `lat`, `lng`, `type`) VALUES (1, 'Cakrudi.com 1', 'Jl. Al Wasliyah,, RT.3/RW.4, Jati, Pulo Gadung, Kota Jakarta Timur,', -6.192359, 106.895950, 'bar'), (2, 'Cakrudi.com 2', 'Jl. Jenderal Ahmad Yani Kav. 49, Rawasari, Cempaka Putih', -6.193842, 106.890160, 'bar'), (3, 'Cakrudi.com 3', 'Jalan Perintis Kemerdekaan Kav. 99, Pulogadung, RT.1/RW.1, RT.1/RW.1', -6.185831, 106.889084, 'bar'); -- -- Indexes for dumped tables -- -- -- Indexes for table `markers` -- ALTER TABLE `markers` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `markers` -- ALTER TABLE `markers` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10; COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
- Langkah terakhir adalah memanggil isi database dengan javascript yang sudah terinclude KEY API map dengan membuat file index.thml seperti dibawah ini.
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 | <!DOCTYPE html > <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Using MySQL and PHP with Google Maps</title> <style> /* Always set the map height explicitly to define the size of the div * element that contains the map. */ #map { height: 100%; } /* Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } </style> </head> <body> <div id="map"></div> <script> var customLabel = { restaurant: { label: 'R' }, bar: { label: 'B' } }; function initMap() { var map = new google.maps.Map(document.getElementById('map'), { center: new google.maps.LatLng(-6.2192359, 106.895950), zoom: 14 }); var infoWindow = new google.maps.InfoWindow; // Change this depending on the name of your PHP or XML file downloadUrl('http://localhost/map/map.php', function(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName('marker'); Array.prototype.forEach.call(markers, function(markerElem) { var name = markerElem.getAttribute('name'); var address = markerElem.getAttribute('address'); var type = markerElem.getAttribute('type'); var point = new google.maps.LatLng( parseFloat(markerElem.getAttribute('lat')), parseFloat(markerElem.getAttribute('lng'))); var infowincontent = document.createElement('div'); var strong = document.createElement('strong'); strong.textContent = name infowincontent.appendChild(strong); infowincontent.appendChild(document.createElement('br')); var text = document.createElement('text'); text.textContent = address infowincontent.appendChild(text); var icon = customLabel[type] || {}; var marker = new google.maps.Marker({ map: map, position: point, label: icon.label }); marker.addListener('click', function() { infoWindow.setContent(infowincontent); infoWindow.open(map, marker); }); }); }); } function downloadUrl(url, callback) { var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest; request.onreadystatechange = function() { if (request.readyState == 4) { request.onreadystatechange = doNothing; callback(request, request.status); } }; request.open('GET', url, true); request.send(null); } function doNothing() {} </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=PASANG_KEY_KAMU&callback=initMap"> </script> </body> </html> |
Terimakasih sudah berkunjung di cakrudi.com
ada demonya gan?
BalasHapus