JavaScript: Add marker function on Google Map
Problem :
You want to create add marker Javascript function to put markers on Google Map canvas. How to do that?
Solution :
Below is a code fragment from previous tutorial on how to convert IP address to location in Golang.
var map;
function showMap(latitude, longitude, ipaddress) {
var pos = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: 5,
center: pos,
mapTypeId: google.maps.MapTypeId.ROADMAP,
content: 'Location found by IP Address'
};
var mapDiv = document.getElementById("map-canvas");
map = new google.maps.Map(mapDiv, mapOptions);
var title = ipaddress + " location";
addMarker(map, pos, title, "");
}
function addMarker(map, latlong, title, content) {
var markerOptions = {
position: latlong,
map: map,
title: title,
clickable: true
};
var marker = new google.maps.Marker(markerOptions);
}
Hope this helps and happy coding!
References :
https://developers.google.com/maps/documentation/javascript/examples/marker-simple
https://www.socketloop.com/tutorials/golang-find-location-by-ip-address-and-display-with-google-map
See also : Golang : Find location by IP address and display with Google Map
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5k Golang : How to search a list of records or data structures
+25.6k Golang : How to check if a connection to database is still alive ?
+12.4k Golang : Get constant name from value
+6.4k Golang : Check to see if *File is a file or directory
+8k Golang : automatically figure out array length(size) with three dots
+7.7k Golang : Combine slices but preserve order example
+5.9k Golang : constant 20013 overflows byte error message
+15.5k Golang : Set up source IP address before making HTTP request
+35.1k Golang : Convert date or time stamp from string to time.Time type
+27.9k Golang : How to create new XML file ?
+6.1k Golang : Squaring elements in array
+15.8k Golang : Get number of CPU cores