2017-11-10 3 views
1

인구를 나타낼 각 국가 이름 근처에 작은 라벨을 추가하려고합니다 (예 : 브라질 [200,0000]). Google지도 JS API로이를 수행 할 수있는 방법이 있습니까? 지도 초기화에 대한Google지도의 국가 이름에 라벨을 어떻게 추가하나요?

내 현재 코드 :

// Define options 
    var options = { 
     center: { 
      lat: 48.1250223, 
      lng: 4.1264001 
     }, 
     zoom: 3 
    }; 

    // Init map 
    map = new google.maps.Map($container.get(0), options); 

는 어떤 도움을 주시면 감사하겠습니다!

+0

해당 위치에 마커를 추가하고 마커에 대한 간단한 텍스트를 추가 할 수 있습니다. – vikscool

+0

@vikscool, 감사합니다. 그러나 저는 사용자 이름 바로 다음에 표시되도록하고 싶었 기 때문에 사용자는 마커를 클릭하거나 수백 개의 마커를 동시에 볼 필요가 없었습니다. – sdvnksv

+0

마커가로드 된지도를로드 할 수 있으며 마커 개체에는 자신 만의 텍스트를 추가 할 수있는 "label"이라는 속성이 있습니다. 자세한 내용을 보려면 https://developers.google.com/maps/documentation/javascript/examples/ 예를 들어 – vikscool

답변

0

구글 API https://developers.google.com/maps/documentation/javascript/combining-data

전체 코드에서이 예제를 확인할 수 있습니다. 간단한 예를 첨부했습니다. 음, 국가 국기를 아이콘으로 사용하고 그 위에 레이블을 보여주십시오.

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
 
    <meta charset="utf-8"> 
 
    <title>Marker with Label</title> 
 
    <style> 
 
     #map {height: 100%;} 
 
     html, body {height: 100%;} 
 
    </style> 
 
    </head> 
 
    <body> 
 
    <div id="map"></div> 
 
    <script> 
 

 
    function initMap() { 
 
     var map = new google.maps.Map(document.getElementById('map'), { 
 
      zoom: 4, 
 
      center: {lat: 48.1, lng: 4.1}, 
 
      mapTypeId: 'terrain' 
 
     }); 
 

 
\t \t //set the text as marker label without displayinge the marker 
 
\t \t var m = new google.maps.Marker({ 
 
\t \t position: {lat: 48.1, lng: 4.1}, 
 
\t \t label: { 
 
\t \t \t color: 'purple', 
 
\t \t \t fontWeight: 'bold', 
 
\t \t \t text: 'people: 67 Mio', 
 
\t \t \t }, 
 
\t \t icon: { 
 
\t \t \t url: 'none_marker.png', 
 
\t \t \t anchor: new google.maps.Point(10, -25), 
 
\t \t }, 
 
\t \t \t map: map 
 
\t \t }); 
 
\t } 
 
    </script> 
 
\t <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script> 
 
    </body> 
 
</html>

+0

좋은 소리입니다, 감사합니다! – sdvnksv

0

인구 통계 데이터는 Google Maps API v3에서 사용할 수 없습니다. 외부 소스에서 데이터를 가져와야합니다.

당신은 당신은 마커를 표시하지 않고 마커의 레이블을 사용할 수

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
 
    <title>Mashups with google.maps.Data</title> 
 
    <style> 
 
     html, body, #map { height: 100%; margin: 0; padding: 0; overflow: hidden; } 
 
     .nicebox { 
 
      position: absolute; 
 
      text-align: center; 
 
      font-family: "Roboto", "Arial", sans-serif; 
 
      font-size: 13px; 
 
      z-index: 5; 
 
      box-shadow: 0 4px 6px -4px #333; 
 
      padding: 5px 10px; 
 
      background: rgb(255,255,255); 
 
      background: linear-gradient(to bottom,rgba(255,255,255,1) 0%,rgba(245,245,245,1) 100%); 
 
      border: rgb(229, 229, 229) 1px solid; 
 
     } 
 
     #controls { 
 
      top: 10px; 
 
      left: 110px; 
 
      width: 360px; 
 
      height: 45px; 
 
     } 
 
     #data-box { 
 
      top: 10px; 
 
      left: 500px; 
 
      height: 45px; 
 
      line-height: 45px; 
 
      display: none; 
 
     } 
 
     #census-variable { 
 
      width: 360px; 
 
      height: 20px; 
 
     } 
 
     #legend { display: flex; display: -webkit-box; padding-top: 7px } 
 
     .color-key { 
 
      background: linear-gradient(to right, 
 
      hsl(5, 69%, 54%) 0%, 
 
      hsl(29, 71%, 51%) 17%, 
 
      hsl(54, 74%, 47%) 33%, 
 
      hsl(78, 76%, 44%) 50%, 
 
      hsl(102, 78%, 41%) 67%, 
 
      hsl(127, 81%, 37%) 83%, 
 
      hsl(151, 83%, 34%) 100%); 
 
      flex: 1; 
 
      -webkit-box-flex: 1; 
 
      margin: 0 5px; 
 
      text-align: left; 
 
      font-size: 1.0em; 
 
      line-height: 1.0em; 
 
     } 
 
     #data-value { font-size: 2.0em; font-weight: bold } 
 
     #data-label { font-size: 2.0em; font-weight: normal; padding-right: 10px; } 
 
     #data-label:after { content: ':' } 
 
     #data-caret { margin-left: -5px; display: none; font-size: 14px; width: 14px} 
 
    </style> 
 
    </head> 
 
    <body> 
 
    <div id="controls" class="nicebox"> 
 
     <div> 
 
     <select id="census-variable"> 
 
     <option value="https://storage.googleapis.com/mapsdevsite/json/DP02_0066PE">Percent of population over 25 that completed high 
 
     school</option> 
 
     <option value="https://storage.googleapis.com/mapsdevsite/json/DP05_0017E">Median age</option> 
 
     <option value="https://storage.googleapis.com/mapsdevsite/json/DP05_0001E">Total population</option> 
 
     <option value="https://storage.googleapis.com/mapsdevsite/json/DP02_0016E">Average family size</option> 
 
     <option value="https://storage.googleapis.com/mapsdevsite/json/DP03_0088E">Per-capita income</option> 
 
     </select> 
 
     </div> 
 
     <div id="legend"> 
 
     <div id="census-min">min</div> 
 
     <div class="color-key"><span id="data-caret">&#x25c6;</span></div> 
 
     <div id="census-max">max</div> 
 
     </div> 
 
    </div> 
 
    <div id="data-box" class="nicebox"> 
 
     <label id="data-label" for="data-value"></label> 
 
     <span id="data-value"></span> 
 
    </div> 
 
    <div id="map"></div> 
 
    <script> 
 
     var mapStyle = [{ 
 
     'stylers': [{'visibility': 'off'}] 
 
     }, { 
 
     'featureType': 'landscape', 
 
     'elementType': 'geometry', 
 
     'stylers': [{'visibility': 'on'}, {'color': '#fcfcfc'}] 
 
     }, { 
 
     'featureType': 'water', 
 
     'elementType': 'geometry', 
 
     'stylers': [{'visibility': 'on'}, {'color': '#bfd4ff'}] 
 
     }]; 
 
     var map; 
 
     var censusMin = Number.MAX_VALUE, censusMax = -Number.MAX_VALUE; 
 

 
     function initMap() { 
 

 
     // load the map 
 
     map = new google.maps.Map(document.getElementById('map'), { 
 
      center: {lat: 40, lng: -100}, 
 
      zoom: 4, 
 
      styles: mapStyle 
 
     }); 
 

 

 
     // set up the style rules and events for google.maps.Data 
 
     map.data.setStyle(styleFeature); 
 
     map.data.addListener('mouseover', mouseInToRegion); 
 
     map.data.addListener('mouseout', mouseOutOfRegion); 
 

 
     // wire up the button 
 
     var selectBox = document.getElementById('census-variable'); 
 
     google.maps.event.addDomListener(selectBox, 'change', function() { 
 
      clearCensusData(); 
 
      loadCensusData(selectBox.options[selectBox.selectedIndex].value); 
 
     }); 
 

 
     // state polygons only need to be loaded once, do them now 
 
     loadMapShapes(); 
 

 
     } 
 

 
     /** Loads the state boundary polygons from a GeoJSON source. */ 
 
     function loadMapShapes() { 
 
     // load US state outline polygons from a GeoJson file 
 
     map.data.loadGeoJson('https://storage.googleapis.com/mapsdevsite/json/states.js', { idPropertyName: 'STATE' }); 
 

 
     // wait for the request to complete by listening for the first feature to be 
 
     // added 
 
     google.maps.event.addListenerOnce(map.data, 'addfeature', function() { 
 
      google.maps.event.trigger(document.getElementById('census-variable'), 
 
       'change'); 
 
     }); 
 
     } 
 

 
     /** 
 
     * Loads the census data from a simulated API call to the US Census API. 
 
     * 
 
     * @param {string} variable 
 
     */ 
 
     function loadCensusData(variable) { 
 
     // load the requested variable from the census API (using local copies) 
 
     var xhr = new XMLHttpRequest(); 
 
     xhr.open('GET', variable + '.json'); 
 
     xhr.onload = function() { 
 
      var censusData = JSON.parse(xhr.responseText); 
 
      censusData.shift(); // the first row contains column names 
 
      censusData.forEach(function(row) { 
 
      var censusVariable = parseFloat(row[0]); 
 
      var stateId = row[1]; 
 

 
      // keep track of min and max values 
 
      if (censusVariable < censusMin) { 
 
       censusMin = censusVariable; 
 
      } 
 
      if (censusVariable > censusMax) { 
 
       censusMax = censusVariable; 
 
      } 
 

 
      // update the existing row with the new data 
 
      map.data 
 
       .getFeatureById(stateId) 
 
       .setProperty('census_variable', censusVariable); 
 
      }); 
 

 
      // update and display the legend 
 
      document.getElementById('census-min').textContent = 
 
       censusMin.toLocaleString(); 
 
      document.getElementById('census-max').textContent = 
 
       censusMax.toLocaleString(); 
 
     }; 
 
     xhr.send(); 
 
     } 
 

 
     /** Removes census data from each shape on the map and resets the UI. */ 
 
     function clearCensusData() { 
 
     censusMin = Number.MAX_VALUE; 
 
     censusMax = -Number.MAX_VALUE; 
 
     map.data.forEach(function(row) { 
 
      row.setProperty('census_variable', undefined); 
 
     }); 
 
     document.getElementById('data-box').style.display = 'none'; 
 
     document.getElementById('data-caret').style.display = 'none'; 
 
     } 
 

 
     /** 
 
     * Applies a gradient style based on the 'census_variable' column. 
 
     * This is the callback passed to data.setStyle() and is called for each row in 
 
     * the data set. Check out the docs for Data.StylingFunction. 
 
     * 
 
     * @param {google.maps.Data.Feature} feature 
 
     */ 
 
     function styleFeature(feature) { 
 
     var low = [5, 69, 54]; // color of smallest datum 
 
     var high = [151, 83, 34]; // color of largest datum 
 

 
     // delta represents where the value sits between the min and max 
 
     var delta = (feature.getProperty('census_variable') - censusMin)/
 
      (censusMax - censusMin); 
 

 
     var color = []; 
 
     for (var i = 0; i < 3; i++) { 
 
      // calculate an integer color based on the delta 
 
      color[i] = (high[i] - low[i]) * delta + low[i]; 
 
     } 
 

 
     // determine whether to show this shape or not 
 
     var showRow = true; 
 
     if (feature.getProperty('census_variable') == null || 
 
      isNaN(feature.getProperty('census_variable'))) { 
 
      showRow = false; 
 
     } 
 

 
     var outlineWeight = 0.5, zIndex = 1; 
 
     if (feature.getProperty('state') === 'hover') { 
 
      outlineWeight = zIndex = 2; 
 
     } 
 

 
     return { 
 
      strokeWeight: outlineWeight, 
 
      strokeColor: '#fff', 
 
      zIndex: zIndex, 
 
      fillColor: 'hsl(' + color[0] + ',' + color[1] + '%,' + color[2] + '%)', 
 
      fillOpacity: 0.75, 
 
      visible: showRow 
 
     }; 
 
     } 
 

 
     /** 
 
     * Responds to the mouse-in event on a map shape (state). 
 
     * 
 
     * @param {?google.maps.MouseEvent} e 
 
     */ 
 
     function mouseInToRegion(e) { 
 
     // set the hover state so the setStyle function can change the border 
 
     e.feature.setProperty('state', 'hover'); 
 

 
     var percent = (e.feature.getProperty('census_variable') - censusMin)/
 
      (censusMax - censusMin) * 100; 
 

 
     // update the label 
 
     document.getElementById('data-label').textContent = 
 
      e.feature.getProperty('NAME'); 
 
     document.getElementById('data-value').textContent = 
 
      e.feature.getProperty('census_variable').toLocaleString(); 
 
     document.getElementById('data-box').style.display = 'block'; 
 
     document.getElementById('data-caret').style.display = 'block'; 
 
     document.getElementById('data-caret').style.paddingLeft = percent + '%'; 
 
     } 
 

 
     /** 
 
     * Responds to the mouse-out event on a map shape (state). 
 
     * 
 
     * @param {?google.maps.MouseEvent} e 
 
     */ 
 
     function mouseOutOfRegion(e) { 
 
     // reset the hover state, returning the border to normal 
 
     e.feature.setProperty('state', 'normal'); 
 
     } 
 

 
    </script> 
 
    <script async defer 
 
     src="https://maps.googleapis.com/maps/api/js?callback=initMap"> 
 
    </script> 
 
    </body> 
 
</html>

+0

을 확인할 수 있습니다.하지만 국가 이름 옆에 라벨을 추가하여 내 맞춤 정보를 표시하는 방법이 있습니다. (예 : 모집단)이 데이터를 내보내는 방법이 아닙니다. 당신이 제공 한 예제는 꼭 필요한 별도의 상자에 꼭 필요한 것을 보여줍니다. – sdvnksv

관련 문제