In the previous tutorial about "Creating Marker on Google Maps", we have discussed how to add google map on the web and create one marker on the location that we want. But the marker that we created is only for one position location (latitude, longtitude).
To create multiple markers on google maps, we have to determine which locations to mark it. A location on map is a value in the form of latitude and longitude coordinates. Then a new iteration to set the marker based on the location we have set.
Here is the expected output at the end of this tutorial, as shown in Figure 1 below:
(Gambar.1)
Step by Step Guide: Create Multiple Marker On Google Maps
1. Loading the Google Maps JavaScript API
This script is required to connect to Google Maps Services
In the sensor paremeter , we set false. This is because we will determine the position of the location itself.
2. Set position coordinates of location (latitude and longitude)
Because we are going to create marker for many locations, so we have to prepare which location that we want to mark it. In this tutorial, we will create a marker for five locations :
// Menentukan lokasi berdasarkan latitude dan longitude
var locations =[['<h4>Ibukota Provinsi Aceh</h4>',5.550176,95.319263],['<h4>Ibukota Kab.Aceh Jaya</h4>',4.727890,95.601373],['<h4>Ibukota Abdya</h4>',3.818570,96.831841],['<h4>Ibukota Kotamadya Langsa</h4>',4.476020,97.952447],['<h4>Ibukota Kotamadya Sabang</h4>',5.909284,95.304742]];
3. Add property to map object
var options ={
zoom:8,
center:newgoogle.maps.LatLng(4.845582,96.271539),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
Please read the tutorial about Google Maps display properties 4. Create a map
This step is to create a map with parameters of the variable / object options above:
// Pembuatan petanya
var map =newgoogle.maps.Map(document.getElementById('map_canvas'), options);
5. Create markers and event clicks
In step 5, we will create a marker for each of the different locations and add an event click for each marker whose function is to display the information that we define.
var marker, i;
for (i = 0; i < locations.length; i++){marker = new google.maps.Marker({position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
});
google.maps.event.addListener(marker, 'click', (function(marker, i){return function(){
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);}})(marker, i));}
Complete Source Code
In this example we will create a marker for five cities: Banda Aceh, Sabang, Calang, Langsa and Blangpidie. Each city will display information when the markers is clicked, as shown in Figure 1 above.
Comment Policy: Silahkan tuliskan komentar Anda yang sesuai dengan topik postingan halaman ini. Komentar yang berisi tautan tidak akan ditampilkan sebelum disetujui.