Google मैप्स JS API v3 - सरल एकाधिक मार्कर उदाहरण


657

Google मानचित्र Api के लिए बहुत नया है। मुझे एक ऐसा डेटा मिला है, जिसे मैं मैप पर साइकिल और प्लॉट करना चाहता हूं। काफी सरल लगता है, लेकिन मेरे द्वारा पाए गए सभी मल्टी-मार्कर ट्यूटोरियल काफी जटिल हैं।

उदाहरण के लिए Google की साइट से डेटा ऐरे का उपयोग करते हैं:

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

मैं बस इन सभी बिंदुओं को प्लॉट करना चाहता हूं और जब नाम प्रदर्शित करने के लिए क्लिक किया जाता है तो एक इन्फोवॉन्ड पॉप अप होता है।

जवाबों:


1128

यह सबसे सरल है जिसे मैं इसे कम कर सकता हूं:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>

  <script type="text/javascript">
    var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    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));
    }
  </script>
</body>
</html>

कोडन → पर संपादित करें / कांटा

SCREENSHOT

गूगल मैप्स कई मार्करों

कॉलबैक तर्क को addListenerविधि में पास करते समय कुछ बंद होने वाला जादू होता है । यह काफी पेचीदा विषय हो सकता है यदि आप इस बात से परिचित नहीं हैं कि क्लोजर कैसे काम करता है। मैं एक संक्षिप्त परिचय के लिए निम्नलिखित मोज़िला लेख की जाँच करने का सुझाव दूंगा यदि यह मामला है:

मोज़िला देव केंद्र: क्लोजर के साथ काम करना


4
@ राफेलडीडीएल: हाँ, उन कोष्ठकों को वास्तव में नामहीन कार्य करने के लिए आवश्यक है। जावास्क्रिप्ट के काम करने के तरीके (बंद होने के कारण) के कारण तर्कों को पारित करने की आवश्यकता है। एक उदाहरण और अधिक जानकारी के लिए इस सवाल का मेरा जवाब देखें: stackoverflow.com/a/2670420/222908
डैनियल वेसलो

अच्छा जवाब है, लेकिन इसे और सरल बनाया जा सकता है। चूंकि सभी मार्करों में अलग-अलग InfoWindows होगी और चूंकि जावास्क्रिप्ट परवाह नहीं करता है यदि आप किसी ऑब्जेक्ट में अतिरिक्त गुण जोड़ते हैं, तो आपको बस InfoWindowमार्कर के गुणों में एक जोड़ने की जरूरत है और फिर .open()खुद से InfoWindow पर कॉल करें । मैंने यहां परिवर्तन पोस्ट किया होगा, लेकिन यह संशोधन काफी बड़े थे कि मैंने अपना जवाब पोस्ट किया
मैथ्यू कॉर्डरो

new MarkerClusterer()बड़े पैमाने पर प्रदर्शन बस्ट के लिए उपयोग क्यों नहीं ? ChirsSwires उत्तर देखें।
DevWL

हाय @ डैनियल वैसलो, मुझे भी अपने आयनिक कोणीय परियोजना में कई मार्करों को प्रदर्शित करने की आवश्यकता है। कृपया मेरी सहायता करें, मैंने पहले ही स्टैकओवरफ़्लो पर एक प्रश्न पूछा। यहाँ प्रश्न लिंक है: stackoverflow.com/questions/57985967/…
सैफ

यह काम करता हैं। धन्यवाद। आप गूगल मैप से मार्करों को कैसे हटाएंगे क्योंकि आप मार्कर के एकल उदाहरण का उपयोग कर रहे हैं और लूप में इनिशियलाइज़ करते हैं। कृपया अपने विचार साझा करें।
कमलेश

59

यहाँ एक अद्वितीय titleऔर infoWindowपाठ के साथ लोड हो रहे कई मार्करों का एक और उदाहरण है । नवीनतम Google नक्शे API V3.11 के साथ परीक्षण किया गया।

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>Multiple Markers Google Maps</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.11&sensor=false" type="text/javascript"></script>
        <script type="text/javascript">
        // check DOM Ready
        $(document).ready(function() {
            // execute
            (function() {
                // map options
                var options = {
                    zoom: 5,
                    center: new google.maps.LatLng(39.909736, -98.522109), // centered US
                    mapTypeId: google.maps.MapTypeId.TERRAIN,
                    mapTypeControl: false
                };

                // init map
                var map = new google.maps.Map(document.getElementById('map_canvas'), options);

                // NY and CA sample Lat / Lng
                var southWest = new google.maps.LatLng(40.744656, -74.005966);
                var northEast = new google.maps.LatLng(34.052234, -118.243685);
                var lngSpan = northEast.lng() - southWest.lng();
                var latSpan = northEast.lat() - southWest.lat();

                // set multiple marker
                for (var i = 0; i < 250; i++) {
                    // init markers
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random()),
                        map: map,
                        title: 'Click Me ' + i
                    });

                    // process multiple info windows
                    (function(marker, i) {
                        // add click event
                        google.maps.event.addListener(marker, 'click', function() {
                            infowindow = new google.maps.InfoWindow({
                                content: 'Hello, World!!'
                            });
                            infowindow.open(map, marker);
                        });
                    })(marker, i);
                }
            })();
        });
        </script>
    </head>
    <body>
        <div id="map_canvas" style="width: 800px; height:500px;"></div>
    </body>
</html>

250 मार्करों का स्क्रीनशॉट:

एकाधिक मार्करों के साथ Google मैप्स एपीआई V3.11

यह स्वचालित रूप से लाट / Lng को अद्वितीय बनाने के लिए यादृच्छिक करेगा। यदि आप 500, 1000, xxx मार्कर और प्रदर्शन का परीक्षण करना चाहते हैं तो यह उदाहरण बहुत उपयोगी होगा।


1
कई प्रश्नों के बायलरप्लेट / वर्बेटिम उत्तर कॉपी और पेस्ट करते समय सावधान रहें, ये समुदाय द्वारा "स्पैम" के रूप में चिह्नित किए जाते हैं। यदि आप ऐसा कर रहे हैं तो इसका आमतौर पर मतलब है कि प्रश्न डुप्लिकेट हैं इसलिए उन्हें इस तरह से चिह्नित करें।
केव

1
यह infoWindowप्रत्येक मार्कर के लिए कई पॉप अप प्राप्त करेगा और infoWindowयदि यह वर्तमान में दिखाया गया है तो दूसरे को छिपाएगा नहीं । यह वास्तव में उपयोगी है :)
कन्निका

@ अनूप, अगर आप सिर्फ सवाल पढ़ें और टिप्पणी करें तो बेहतर होगा। सवाल "कई मार्कर के लिए उदाहरण" पूछ रहा है कि क्या यह यादृच्छिक है या आपका अपना ब्ला ब्ला।
मदन सपकोटा

फिर new MarkerClusterer()बड़े पैमाने पर प्रदर्शन के लिए उपयोग क्यों नहीं किया गया ? ChirsSwires उत्तर देखें।
DevWL

1
@DevWL, इसे 2013 में वापस जवाब दिया गया था। आप अपडेट करने के लिए स्वतंत्र हैं।
मदन सपकोटा

39

मुझे लगा कि मैं इसे यहां डालूंगा क्योंकि यह Google मैप्स एपीआई का उपयोग करने वालों के लिए एक लोकप्रिय लैंडिंग बिंदु प्रतीत होता है। क्लाइंट की ओर से प्रदान किए गए एकाधिक मार्कर संभवतः कई मैपिंग अनुप्रयोगों के प्रदर्शन के लिहाज से कम हैं। बेंचमार्क करना, ठीक करना मुश्किल है और कुछ मामलों में यह भी स्थापित है कि कोई समस्या है (ब्राउज़र कार्यान्वयन अंतर के कारण, क्लाइंट के लिए उपलब्ध हार्डवेयर, मोबाइल डिवाइस, सूची चालू हो जाती है)।

इस समस्या का समाधान करने का सबसे सरल तरीका एक मार्कर क्लस्टरिंग समाधान का उपयोग करना है। मूल विचार भौगोलिक रूप से समान स्थानों को समूह में समूह को प्रदर्शित किए गए अंकों की संख्या के साथ समूह में रखना है। जैसा कि उपयोगकर्ता मानचित्र में बताता है कि ये समूह अलग-अलग मार्करों को प्रकट करने के लिए विस्तारित होते हैं।

शायद लागू करने के लिए सबसे सरल मार्करलर लाइब्रेरी है। एक बुनियादी कार्यान्वयन निम्नानुसार होगा (पुस्तकालय आयात के बाद):

<script type="text/javascript">
  function initialize() {
    var center = new google.maps.LatLng(37.4419, -122.1419);

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 3,
      center: center,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var markers = [];
    for (var i = 0; i < 100; i++) {
      var location = yourData.location[i];
      var latLng = new google.maps.LatLng(location.latitude,
          location.longitude);
      var marker = new google.maps.Marker({
        position: latLng
      });
      markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers);
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

मानचित्र में सीधे जोड़े जाने के बजाय मार्करों को एक सरणी में जोड़ा जाता है। इस सरणी को फिर लाइब्रेरी में पास किया जाता है जो आपके लिए जटिल गणना को संभालती है और मैप से जुड़ी होती है।

न केवल ये कार्यान्वयन बड़े पैमाने पर क्लाइंट साइड प्रदर्शन को बढ़ाते हैं, बल्कि कई मामलों में बड़े पैमाने पर सरल और कम बरबाद यूआई और डेटा के आसान पाचन के लिए नेतृत्व करते हैं।

अन्य कार्यान्वयन Google से उपलब्ध हैं।

आशा है कि यह उन लोगों में से कुछ को मैपिंग की बारीकियों से जोड़ता है।


2
धन्यवाद, एक बड़ी मदद! प्रदर्शन में एक आदेश या परिमाण अंतर है, पहले google.map डेटा बिंदुओं को बनाकर और फिर इसे मैपिंग लाइब्रेरी में पास किया जाता है, इस मामले में MarketCluster से प्लॉट करने के लिए। लगभग 150,000 डेटा बिंदुओं के साथ 'डैनियल वासलो' द्वारा पहली पोस्ट को लोड करने में लगभग 2 मिनट लगे, यह 5 सेकंड है। धन्यवाद एक गुच्छा 'स्वियर'!
वकास

1
मैंने सोचा कि यह इसके लिए एक अच्छी जगह होगी, मैं कल्पना करूंगा कि ज्यादातर लोग पहले स्टैक पर उतरेंगे जब Google मैप्स इस पेज से संबंधित होगा, और दूसरा है कि 'मेरा मैप लोड होने में इतना लंबा समय क्यों लेता है।'
ChrisSwires

@ यह आपका डेटा सेट जो भी है, यह एक प्लेसहोल्डर चर है।
क्रिसवियर्स

20

अतुल्यकालिक संस्करण:

<script type="text/javascript">
  function initialize() {
    var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    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));
    }
}

function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' +
      'callback=initialize';
  document.body.appendChild(script);
}

window.onload = loadScript;
  </script>

15

यह वर्किंग उदाहरण मानचित्र छवि है

var arr = new Array();
    function initialize() { 
        var i;  
        var Locations = [
                {
                  lat:48.856614, 
                  lon:2.3522219000000177, 
                  address:'Paris',
                  gval:'25.5',
                  aType:'Non-Commodity',
                  title:'Paris',
                  descr:'Paris'           
                },        
                    {
                  lat: 55.7512419, 
                  lon: 37.6184217,
                  address:'Moscow',
                  gval:'11.5',
                  aType:'Non-Commodity',
                  title:'Moscow',
                  descr:'Moscow Airport'              
                },     

                {
              lat:-9.481553000000002, 
              lon:147.190242, 
              address:'Port Moresby',
              gval:'1',
              aType:'Oil',
              title:'Papua New Guinea',
              descr:'Papua New Guinea 123123123'              
            },
            {
           lat:20.5200,
           lon:77.7500,
           address:'Indore',
            gval:'1',
            aType:'Oil',
            title:'Indore, India',
            descr:'Airport India'
        }
    ];

    var myOptions = {
        zoom: 2,
        center: new google.maps.LatLng(51.9000,8.4731),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    var infowindow =  new google.maps.InfoWindow({
        content: ''
    });

    for (i = 0; i < Locations.length; i++) {
            size=15;        
            var img=new google.maps.MarkerImage('marker.png',           
                new google.maps.Size(size, size),
                new google.maps.Point(0,0),
                new google.maps.Point(size/2, size/2)
           );

        var marker = new google.maps.Marker({
            map: map,
            title: Locations[i].title,
            position: new google.maps.LatLng(Locations[i].lat, Locations[i].lon),           
                icon: img
        });

        bindInfoWindow(marker, map, infowindow, "<p>" + Locations[i].descr + "</p>",Locations[i].title);  

    }

}

function bindInfoWindow(marker, map, infowindow, html, Ltitle) { 
    google.maps.event.addListener(marker, 'mouseover', function() {
            infowindow.setContent(html); 
            infowindow.open(map, marker); 

    });
    google.maps.event.addListener(marker, 'mouseout', function() {
        infowindow.close();

    }); 
} 

पूर्ण कार्य उदाहरण। आप बस कॉपी, पेस्ट और उपयोग कर सकते हैं।


12

से गूगल मानचित्र एपीआई के नमूने :

function initialize() {
  var myOptions = {
    zoom: 10,
    center: new google.maps.LatLng(-33.9, 151.2),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"),
                                myOptions);

  setMarkers(map, beaches);
}

/**
 * Data for the markers consisting of a name, a LatLng and a zIndex for
 * the order in which these markers should display on top of each
 * other.
 */
var beaches = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

function setMarkers(map, locations) {
  // Add markers to the map

  // Marker sizes are expressed as a Size of X,Y
  // where the origin of the image (0,0) is located
  // in the top left of the image.

  // Origins, anchor positions and coordinates of the marker
  // increase in the X direction to the right and in
  // the Y direction down.
  var image = new google.maps.MarkerImage('images/beachflag.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(20, 32),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(0, 32));
  var shadow = new google.maps.MarkerImage('images/beachflag_shadow.png',
      // The shadow image is larger in the horizontal dimension
      // while the position and offset are the same as for the main image.
      new google.maps.Size(37, 32),
      new google.maps.Point(0,0),
      new google.maps.Point(0, 32));
      // Shapes define the clickable region of the icon.
      // The type defines an HTML &lt;area&gt; element 'poly' which
      // traces out a polygon as a series of X,Y points. The final
      // coordinate closes the poly by connecting to the first
      // coordinate.
  var shape = {
      coord: [1, 1, 1, 20, 18, 20, 18 , 1],
      type: 'poly'
  };
  for (var i = 0; i < locations.length; i++) {
    var beach = locations[i];
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        shadow: shadow,
        icon: image,
        shape: shape,
        title: beach[0],
        zIndex: beach[3]
    });
  }
}

8
इस जवाब में
infoWindow

@omat अजीब तरह से Google के अपने डॉक्स का सुझाव नहीं है कि वहाँ एक जानकारी का हिस्सा होना चाहिए। लेकिन फिर भी यह मेरे लिए काम नहीं कर रहा है :(
एमिल अहलबर्क

11

यहाँ एक और संस्करण है जो मैंने मानचित्र रियल एस्टेट को बचाने के लिए लिखा था, जो कि इनफ्लोइंडो पॉइंटर को वास्तविक लेट और मार्कर के लंबे स्थान पर रखता है, जबकि अस्थायी रूप से मार्कर को छिपाते समय, जबकि इनवॉइंडो को प्रदर्शित किया जा रहा है।

यह मानक 'मार्कर' असाइनमेंट के साथ दूर भी करता है और मार्कर के निर्माण पर मार्कर सरणी के लिए नए मार्कर को सीधे असाइन करके प्रसंस्करण को गति देता है। हालाँकि, ध्यान दें कि अतिरिक्त गुणों को मार्कर और इनफाइंडो दोनों में जोड़ा गया है, इसलिए यह दृष्टिकोण एक अपरंपरागत है ... लेकिन यह मुझे है!

इन infowindow प्रश्नों में यह उल्लेख नहीं किया गया है, कि मानक infowindow IS को बिंदु के अक्षांश और कोण पर नहीं रखा गया है, बल्कि मार्कर छवि के शीर्ष पर है। काम करने के लिए मार्कर दृश्यता को छिपाया जाना चाहिए, अन्यथा मैप्स एपीआई infowindow लंगर को फिर से मार्कर छवि के शीर्ष पर वापस भेज देगा।

'मार्कर' एरे में मार्करों का संदर्भ किसी भी अतिरिक्त प्रसंस्करण कार्यों के लिए मार्कर घोषणा पर तुरंत बनाया जाता है जो बाद में वांछित हो सकते हैं (छिपाना / दिखाना, अंगारों को पकड़ना, आदि ...)। यह मार्कर ऑब्जेक्ट को 'मार्कर' को निर्दिष्ट करने के अतिरिक्त चरण को बचाता है, और फिर मार्कर मार्कर को 'मार्कर' को धकेलता है ... मेरी पुस्तक में बहुत सारे अनावश्यक प्रसंस्करण।

वैसे भी, infowindows पर एक अलग ले, और आशा है कि यह आपको सूचित और प्रेरित करने में मदद करता है।

    var locations = [
      ['Bondi Beach', -33.890542, 151.274856, 4],
      ['Coogee Beach', -33.923036, 151.259052, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];
    var map;
    var markers = [];

    function init(){
      map = new google.maps.Map(document.getElementById('map_canvas'), {
        zoom: 10,
        center: new google.maps.LatLng(-33.92, 151.25),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      var num_markers = locations.length;
      for (var i = 0; i < num_markers; i++) {  
        markers[i] = new google.maps.Marker({
          position: {lat:locations[i][1], lng:locations[i][2]},
          map: map,
          html: locations[i][0],
          id: i,
        });

        google.maps.event.addListener(markers[i], 'click', function(){
          var infowindow = new google.maps.InfoWindow({
            id: this.id,
            content:this.html,
            position:this.getPosition()
          });
          google.maps.event.addListenerOnce(infowindow, 'closeclick', function(){
            markers[this.id].setVisible(true);
          });
          this.setVisible(false);
          infowindow.open(map);
        });
      }
    }

google.maps.event.addDomListener(window, 'load', init);

यहाँ एक काम JSFiddle है

अतिरिक्त नोट
आप इस दिए गए Google उदाहरण डेटा को एक नंबर के साथ 'स्थानों' सरणी में चौथे स्थान पर देखेंगे। उदाहरण में इसे देखते हुए, आप वर्तमान मूल्य के स्थान पर मार्कर आईडी के लिए इस मान का उपयोग कर सकते हैं, जैसे कि ...

var num_markers = locations.length;
for (var i = 0; i < num_markers; i++) {  
  markers[i] = new google.maps.Marker({
    position: {lat:locations[i][1], lng:locations[i][2]},
    map: map,
    html: locations[i][0],
    id: locations[i][3],
  });
};

10

स्वीकृत उत्तर, ES6 में पुनः लिखा गया:

$(document).ready(() => {
  const mapEl = $('#our_map').get(0); // OR document.getElementById('our_map');

  // Display a map on the page
  const map = new google.maps.Map(mapEl, { mapTypeId: 'roadmap' });

  const buildings = [
    {
      title: 'London Eye, London', 
      coordinates: [51.503454, -0.119562],
      info: 'carousel'
    },
    {
      title: 'Palace of Westminster, London', 
      coordinates: [51.499633, -0.124755],
      info: 'palace'
    }
  ];

  placeBuildingsOnMap(buildings, map);
});


const placeBuildingsOnMap = (buildings, map) => {
  // Loop through our array of buildings & place each one on the map  
  const bounds = new google.maps.LatLngBounds();
  buildings.forEach((building) => {
    const position = { lat: building.coordinates[0], lng: building.coordinates[1] }
    // Stretch our bounds to the newly found marker position
    bounds.extend(position);

    const marker = new google.maps.Marker({
      position: position,
      map: map,
      title: building.title
    });

    const infoWindow = new google.maps.InfoWindow();
    // Allow each marker to have an info window
    google.maps.event.addListener(marker, 'click', () => {
      infoWindow.setContent(building.info);
      infoWindow.open(map, marker);
    })

    // Automatically center the map fitting all markers on the screen
    map.fitBounds(bounds);
  })
})

6

स्रोत लिंक

डेमो लिंक

HTML कोड को पूरा करें

  • क्लिक या होवर पर जानकारी दिखाएं।
  • केवल एक InfoWindow दिखाया जाएगा

यहाँ छवि विवरण दर्ज करें

    <!DOCTYPE html>
    <html>

    <head>
        <style>
            /*  <span class="metadata-marker" style="display: none;" data-region_tag="css"></span>       Set the size of the div element that contains the map */
            #map {
                height: 400px;
                /* The height is 400 pixels */
                width: 100%;
                /* The width is the width of the web page */
            }
        </style>
        <script>
            var map;
            var InforObj = [];
            var centerCords = {
                lat: -25.344,
                lng: 131.036
            };
            var markersOnMap = [{
                    placeName: "Australia (Uluru)",
                    LatLng: [{
                        lat: -25.344,
                        lng: 131.036
                    }]
                },
                {
                    placeName: "Australia (Melbourne)",
                    LatLng: [{
                        lat: -37.852086,
                        lng: 504.985963
                    }]
                },
                {
                    placeName: "Australia (Canberra)",
                    LatLng: [{
                        lat: -35.299085,
                        lng: 509.109615
                    }]
                },
                {
                    placeName: "Australia (Gold Coast)",
                    LatLng: [{
                        lat: -28.013044,
                        lng: 513.425586
                    }]
                },
                {
                    placeName: "Australia (Perth)",
                    LatLng: [{
                        lat: -31.951994,
                        lng: 475.858081
                    }]
                }
            ];

            window.onload = function () {
                initMap();
            };

            function addMarkerInfo() {
                for (var i = 0; i < markersOnMap.length; i++) {
                    var contentString = '<div id="content"><h1>' + markersOnMap[i].placeName +
                        '</h1><p>Lorem ipsum dolor sit amet, vix mutat posse suscipit id, vel ea tantas omittam detraxit.</p></div>';

                    const marker = new google.maps.Marker({
                        position: markersOnMap[i].LatLng[0],
                        map: map
                    });

                    const infowindow = new google.maps.InfoWindow({
                        content: contentString,
                        maxWidth: 200
                    });

                    marker.addListener('click', function () {
                        closeOtherInfo();
                        infowindow.open(marker.get('map'), marker);
                        InforObj[0] = infowindow;
                    });
                    // marker.addListener('mouseover', function () {
                    //     closeOtherInfo();
                    //     infowindow.open(marker.get('map'), marker);
                    //     InforObj[0] = infowindow;
                    // });
                    // marker.addListener('mouseout', function () {
                    //     closeOtherInfo();
                    //     infowindow.close();
                    //     InforObj[0] = infowindow;
                    // });
                }
            }

            function closeOtherInfo() {
                if (InforObj.length > 0) {
                    /* detach the info-window from the marker ... undocumented in the API docs */
                    InforObj[0].set("marker", null);
                    /* and close it */
                    InforObj[0].close();
                    /* blank the array */
                    InforObj.length = 0;
                }
            }

            function initMap() {
                map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 4,
                    center: centerCords
                });
                addMarkerInfo();
            }
        </script>
    </head>

    <body>
        <h3>My Google Maps Demo</h3>
        <!--The div element for the map -->
        <div id="map"></div>

        <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>

    </body>

    </html>

1
के लिए धन्यवाद closeOtherInfo, मैं आपके जवाब तक merkercluster के साथ काम करने के लिए एक अच्छा समाधान नहीं मिल सका। :)
क्रिस loughnane

1
अब वह वही है जिसकी मुझे तलाश थी। धन्यवाद आदमी 2020 में महान काम
फैजान Anwer अली रूपानी

5

अपने प्रोग्राम में एक मार्कर जोड़ना बहुत आसान है। आप बस इस कोड को जोड़ सकते हैं:

var marker = new google.maps.Marker({
  position: myLatLng,
  map: map,
  title: 'Hello World!'
});

जब आप एक मार्कर का निर्माण करते हैं तो निम्नलिखित क्षेत्र विशेष रूप से महत्वपूर्ण और सामान्य रूप से सेट होते हैं:

  • position(आवश्यक) मार्कर के प्रारंभिक स्थान की पहचान करने वाले एक लैटलिंग को निर्दिष्ट करता है। लाटलिंग प्राप्त करने का एक तरीका जियोकोडिंग सेवा का उपयोग करना है
  • map(वैकल्पिक) मार्कर को रखने के लिए मानचित्र को निर्दिष्ट करता है। यदि आप मार्कर के निर्माण पर एक नक्शा निर्दिष्ट नहीं करते हैं, तो मार्कर बनाया जाता है लेकिन नक्शे से जुड़ा नहीं है (या प्रदर्शित नहीं है)। मार्कर की setMap()विधि को कॉल करके आप मार्कर को बाद में जोड़ सकते हैं ।

ध्यान दें , उदाहरण में, शीर्षक फ़ील्ड ने मार्कर का शीर्षक सेट किया है जो टूलटिप के रूप में दिखाई देगा।

आप यहां Google api documenation से परामर्श कर सकते हैं


यह स्थापित करने के लिए एक पूरा उदाहरण है एक नक्शे में मार्कर। पूर्ण देखभाल करें, आपको YOUR_API_KEYअपनी Google API कुंजी को बदलना होगा :

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
   <meta charset="utf-8">
   <title>Simple markers</title>
<style>
  /* Always set the map height explicitly to define the size of the div
   * element that contains the map. */
  #map {
    height: 100%;
  }
  /* Optional: Makes the sample page fill the window. */
  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
</style>
</head>
<body>
 <div id="map"></div>
<script>

  function initMap() {
    var myLatLng = {lat: -25.363, lng: 131.044};

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 4,
      center: myLatLng
    });

    var marker = new google.maps.Marker({
      position: myLatLng,
      map: map,
      title: 'Hello World!'
    });
  }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>


अब, यदि आप किसी मैप में किसी ऐरे के मार्कर को प्लॉट करना चाहते हैं, तो आपको इस तरह करना चाहिए:

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

function initMap() {
  var myLatLng = {lat: -33.90, lng: 151.16};

  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 10,
    center: myLatLng
    });

  var count;

  for (count = 0; count < locations.length; count++) {  
    new google.maps.Marker({
      position: new google.maps.LatLng(locations[count][1], locations[count][2]),
      map: map,
      title: locations[count][0]
      });
   }
}

यह उदाहरण मुझे निम्नलिखित परिणाम देता है:

यहाँ छवि विवरण दर्ज करें


आप अपने पिन में एक जानकारी भी जोड़ सकते हैं। आपको इस कोड की आवश्यकता है:

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[count][1], locations[count][2]),
    map: map
    });

marker.info = new google.maps.InfoWindow({
    content: 'Hello World!'
    });

आपके पास Google के डॉक्यूमेंट यहाँ infoWindows के बारे में हो सकते हैं ।


जब मार्कर इस तरह "क्लिक" होता है, तो अब हम जानकारी खोल सकते हैं:

var marker = new google.maps.Marker({
     position: new google.maps.LatLng(locations[count][1], locations[count][2]),
     map: map
     });

marker.info = new google.maps.InfoWindow({
     content: locations [count][0]
     });


google.maps.event.addListener(marker, 'click', function() {  
    // this = marker
    var marker_map = this.getMap();
    this.info.open(marker_map, this);
    // Note: If you call open() without passing a marker, the InfoWindow will use the position specified upon construction through the InfoWindowOptions object literal.
            });

नोट , आप के बारे में कुछ प्रलेखन हो सकता है Listener यहाँ गूगल डेवलपर में।


और, अंत में, हम एक मार्कर में एक सूचना का निर्माण कर सकते हैं यदि उपयोगकर्ता उस पर क्लिक करता है। यह मेरा पूरा कोड है:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Info windows</title>
    <style>
    /* Always set the map height explicitly to define the size of the div
    * element that contains the map. */
    #map {
        height: 100%;
    }
    /* Optional: Makes the sample page fill the window. */
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }
    </style>
</head>
<body>
    <div id="map"></div>
    <script>

    var locations = [
        ['Bondi Beach', -33.890542, 151.274856, 4],
        ['Coogee Beach', -33.923036, 151.259052, 5],
        ['Cronulla Beach', -34.028249, 151.157507, 3],
        ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
        ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];


    // When the user clicks the marker, an info window opens.

    function initMap() {
        var myLatLng = {lat: -33.90, lng: 151.16};

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            center: myLatLng
            });

        var count=0;


        for (count = 0; count < locations.length; count++) {  

            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[count][1], locations[count][2]),
                map: map
                });

            marker.info = new google.maps.InfoWindow({
                content: locations [count][0]
                });


            google.maps.event.addListener(marker, 'click', function() {  
                // this = marker
                var marker_map = this.getMap();
                this.info.open(marker_map, this);
                // Note: If you call open() without passing a marker, the InfoWindow will use the position specified upon construction through the InfoWindowOptions object literal.
                });
        }
    }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
</body>
</html>

आम तौर पर, आपके पास यह परिणाम होना चाहिए:

आपका परिणाम


4

से बाद डैनियल वसालो के जवाब , यहाँ एक संस्करण है कि एक सरल तरीका था बंद मुद्दे के साथ संबंधित है।

सभी मार्करों के बाद के बाद से एक व्यक्ति होगा InfoWindow और जावास्क्रिप्ट के बाद से परवाह नहीं करता है, तो आप एक वस्तु से अतिरिक्त गुण जोड़ने के लिए, तुम सब करने की जरूरत है एक जोड़ है InfoWindow लिए मार्कर के गुण और फिर फोन .open()पर InfoWindow से ही!

संपादित करें: पर्याप्त डेटा के साथ, पगेलॉड को बहुत समय लग सकता है, इसलिए मार्कर के साथ InfoWindow के निर्माण के बजाय, निर्माण केवल तभी होना चाहिए जब जरूरत हो। ध्यान दें कि InfoWindow के निर्माण के लिए उपयोग किए जाने वाले किसी भी डेटा को मार्कर को एक संपत्ति ( data) के रूप में जोड़ा जाना चाहिए । यह भी ध्यान दें कि पहली क्लिक घटना के बाद, infoWindowयह मार्कर की संपत्ति के रूप में बना रहेगा , इसलिए ब्राउज़र को लगातार पुनर्निर्माण की आवश्यकता नहीं है।

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

var map = new google.maps.Map(document.getElementById('map'), {
  center: new google.maps.LatLng(-33.92, 151.25)
});

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,
    data: {
      name: locations[i][0]
    }
  });
  marker.addListener('click', function() {
    if(!this.infoWindow) {
      this.infoWindow = new google.maps.InfoWindow({
        content: this.data.name;
      });
    }
    this.infoWindow.open(map,this);
  })
}

2

यहाँ एक संपूर्ण उदाहरण जावास्क्रिप्ट फ़ंक्शन है जो JSONObject में परिभाषित कई मार्करों को अनुमति देगा।

यह केवल उन मार्करों को प्रदर्शित करेगा जो मानचित्र की सीमा में हैं।

यह महत्वपूर्ण है इसलिए आप अतिरिक्त काम नहीं कर रहे हैं।

आप मार्करों की एक सीमा भी निर्धारित कर सकते हैं, ताकि आप अत्यधिक मात्रा में मार्कर नहीं दिखा रहे हैं (यदि आपके उपयोग में किसी चीज़ की संभावना है);

यदि मानचित्र का केंद्र 500 मीटर से अधिक नहीं बदला है, तो यह मार्करों को भी प्रदर्शित नहीं करेगा।
यह महत्वपूर्ण है क्योंकि यदि कोई उपयोगकर्ता मार्कर पर क्लिक करता है और गलती से नक्शा खींचता है तो ऐसा करते समय आप नहीं चाहते कि मानचित्र मार्करों को फिर से लोड करे।

मैंने इस फ़ंक्शन को मानचित्र के लिए निष्क्रिय ईवेंट श्रोता से जोड़ा है इसलिए मार्कर केवल तभी दिखाई देंगे जब मानचित्र निष्क्रिय हो और किसी भिन्न ईवेंट के बाद मार्कर को फिर से दिखाएगा।

एक्शन स्क्रीन शॉट में स्क्रीन शॉट में थोड़ा बदलाव होता है, जो इनफिनडोव में अधिक कंटेंट दिखाता है। यहाँ छवि विवरण दर्ज करें pastbin.com से चिपकाया गया

<script src="//pastebin.com/embed_js/uWAbRxfg"></script>


0

यहाँ Reactjs में कई मार्करों का एक उदाहरण है ।

यहाँ छवि विवरण दर्ज करें

नीचे नक्शा घटक है

import React from 'react';
import PropTypes from 'prop-types';
import { Map, InfoWindow, Marker, GoogleApiWrapper } from 'google-maps-react';

const MapContainer = (props) => {
  const [mapConfigurations, setMapConfigurations] = useState({
    showingInfoWindow: false,
    activeMarker: {},
    selectedPlace: {}
  });

  var points = [
    { lat: 42.02, lng: -77.01 },
    { lat: 42.03, lng: -77.02 },
    { lat: 41.03, lng: -77.04 },
    { lat: 42.05, lng: -77.02 }
  ]
  const onMarkerClick = (newProps, marker) => {};

  if (!props.google) {
    return <div>Loading...</div>;
  }

  return (
    <div className="custom-map-container">
      <Map
        style={{
          minWidth: '200px',
          minHeight: '140px',
          width: '100%',
          height: '100%',
          position: 'relative'
        }}
        initialCenter={{
          lat: 42.39,
          lng: -72.52
        }}
        google={props.google}
        zoom={16}
      >
        {points.map(coordinates => (
          <Marker
            position={{ lat: coordinates.lat, lng: coordinates.lng }}
            onClick={onMarkerClick}
            icon={{
              url: 'https://res.cloudinary.com/mybukka/image/upload/c_scale,r_50,w_30,h_30/v1580550858/yaiwq492u1lwuy2lb9ua.png',
            anchor: new google.maps.Point(32, 32), // eslint-disable-line
            scaledSize: new google.maps.Size(30, 30)  // eslint-disable-line
            }}
            name={name}
          />))}
        <InfoWindow
          marker={mapConfigurations.activeMarker}
          visible={mapConfigurations.showingInfoWindow}
        >
          <div>
            <h1>{mapConfigurations.selectedPlace.name}</h1>
          </div>
        </InfoWindow>
      </Map>
    </div>
  );
};

export default GoogleApiWrapper({
  apiKey: process.env.GOOGLE_API_KEY,
  v: '3'
})(MapContainer);

MapContainer.propTypes = {
  google: PropTypes.shape({}).isRequired,
};
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.