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
+12k Golang : Display list of countries and ISO codes
+8.8k Golang : How to use Gorilla webtoolkit context package properly
+14.9k Golang : Get HTTP protocol version example
+15k Golang : Get all local users and print out their home directory, description and group id
+7.6k Golang : Load DSA public key from file example
+8.4k Golang : How to join strings?
+29k Golang : Save map/struct to JSON or XML file
+11.7k Golang : Clean formatting/indenting or pretty print JSON result
+11.8k Golang : Sort and reverse sort a slice of runes
+35.7k Golang : Save image to PNG, JPEG or GIF format.
+29k Golang : JQuery AJAX post data to server and send data back to client example
+9.1k Golang : Timeout example