How do I show multiple locations in Google Maps?
21,308
You iterate over your array, and create a new Marker
instance for each pair. It's simple:
<script>
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(55.378051, -3.435973),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 8
});
var locations = [
new google.maps.LatLng(54.97784, -1.612916),
new google.maps.LatLng(55.378051, -3.435973])
// and additional coordinates, just add a new item
];
locations.forEach(function (location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
});
</script>
This works for any number of latitude/longitude pairs; just add a new item to the locations
array.
Author by
Jun
Updated on November 14, 2020Comments
-
Jun over 3 years
I have multiple longtitude and attitude data and I want to show them in the google map with pins.
How to use the google map API to do this?
-
Jun over 12 yearsGood idea! My case is that I have bunch of location names which need to translated to coordinates. Those location names are extracted from documents whic are search results of search engines. The input are free text of documents and the output is a map with pins showing those location names. This javascript only takes array (fixed database) as input, can it be done in the above situation? Thanks a lot.
-
Jun over 12 yearsI am currently trying to extract geo-location names in the result documents returned by search engine and display them like pins in the map. So there are location names I have which need to be translated to coordinates and then call google map to display the image. Any idea?
-
Umesh Awasthi over 12 yearscan you elaborate your requirement since in your question you told you have latitude an longitude so its really not difficult to call Google map service and passing a point to it, point is nothing but being calculated based on latitude and longitude.
-
Martin Bean over 12 yearsAre your location names is any specific format, or are they literally lifted as text from search engine results in whatever format they were written in the original documents?
-
Jun over 12 yearsFor example, in the search result of google, there are text documents which may contain geo-locations. First I want to find those locations and then maybe call web service to translate names to cordinates stored somewhere if possible. After that, calling Google maps API to show those names as pins in the map.
-
Jun over 12 yearsFor example, in the search result of google, there are text documents which may contain geo-locations. First I want to find those locations and then maybe call web service to translate names to cordinates stored somewhere if possible. After that, calling Google maps API to show those names as pins in the map.
-
Martin Bean over 12 yearsSo you want to replicate Google's search results page?
-
Jun over 12 yearsKind of. I need to find geo-location names in one result document with the database of geo-location names. This is the basis of everything. Any idea?
-
Martin Bean over 12 yearsIf it's user-submitted data then it could be in any format; that's going to take some serious data-mining skills and something that exceeds what can be described in a Stack Overflow comment.