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.

Share:
21,308
Jun
Author by

Jun

Updated on November 14, 2020

Comments

  • Jun
    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
    Jun over 12 years
    Good 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
    Jun over 12 years
    I 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
    Umesh Awasthi over 12 years
    can 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
    Martin Bean over 12 years
    Are 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
    Jun over 12 years
    For 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
    Jun over 12 years
    For 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
    Martin Bean over 12 years
    So you want to replicate Google's search results page?
  • Jun
    Jun over 12 years
    Kind 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
    Martin Bean over 12 years
    If 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.