Я пытаюсь передать переменные функции кнопки onClick из InfoWindow? - PullRequest
0 голосов
/ 14 октября 2019

Я посмотрел примеры этого, но не нашел решения для передачи переменной окружности в мою функцию нажатия кнопки, встроенную в информационное окно Google Maps

Эта строка работает:

var theCircleButton = "<input type='button' onClick='console.log(" + lat + "," + lon + ")' class='btn' value='Show Circle'></input>";

Хотя этой строки нет:

var theCircleButton = "<input type='button' onClick='console.log(" + lat + "," + lon + "," + circle + ")' class='btn' value='Show Circle'></input>";

Поэтому, когда я пытаюсь сделать круг видимым со следующей строкой, он не работает

var theCircleButton = "<input type='button' onClick='circleVisible(" + circle + ")' class='btn' value='Show Circle'></input>";

Это функциональный код

function circleVisible(circle) {
  circle.setVisible(true);
}

function circleInvisible(circle) {
  circle.setVisible(false);
}

function fillMap(map, lat, lon) {

  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lon),
    map: map,
    label: "My Marker"
  });

  var circle = new google.maps.Circle({
    strokeWeight: 1,
    fillOpacity: 0.0,
    map: map,
    visible: false,
    center: new google.maps.LatLng(lat, lon),
    radius: 10000
  });

  var infoWindow = new google.maps.InfoWindow({
      position: marker.getPosition()
  });

  //var theCircleButton = "<input type='button' onClick='console.log(" + lat + "," + lon + ")' class='btn' value='Show Circle'></input>";
  //var theCircleButton = "<input type='button' onClick='console.log(" + lat + "," + lon + "," + circle + ")' class='btn' value='Show Circle'></input>";
  var theCircleButton = "<input type='button' onClick='circleVisible(" + circle + ")' class='btn' value='Show Circle'></input>";


  infoWindow.setContent("<div style='text-align:center;'>" + theCircleButton + "</div>");

  google.maps.event.addListener(marker, "click", function() {
    infoWindow.open(map, marker);
  });

}


function initMap() {
  // Latitude and Longitude for the centre of Australia
  var centreAusLat = -28.080606;
  var centreAusLon = 133.104225;
  var zoomLevelAus = 4;

  // the options used to create the Google Maps object
  var mapOptions = {
    // roughly the centre of Australia
    center: new google.maps.LatLng(centreAusLat, centreAusLon),

    // zoom level set to see all of Australia
    zoom: zoomLevelAus,

    // show a satellite image based map
    mapTypeId: google.maps.MapTypeId.SATELLITE,
  };

  // create the Google Maps object
  var map = new google.maps.Map(
      document.getElementById('map-canvas'), mapOptions
  );

  fillMap(map, centreAusLat, centreAusLon);
}

1 Ответ

0 голосов
/ 14 октября 2019

Переменная circle должна быть определена в глобальном контексте, чтобы быть доступной в функциях прослушивания щелчков HTML.

// in global context.
var circle;
function fillMap(map, lat, lon) {
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lon),
    map: map,
    label: "My Marker"
  });

  circle = new google.maps.Circle({
    strokeWeight: 1,
    fillOpacity: 0.0,
    map: map,
    visible: false,
    center: new google.maps.LatLng(lat, lon),
    radius: 10000
  });

подтверждение концепции скрипта

screenshot of resulting map with circle

var circle;

function fillMap(map, lat, lon) {
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lon),
    map: map,
    label: "My Marker"
  });

  circle = new google.maps.Circle({
    strokeWeight: 1,
    fillOpacity: 0.0,
    map: map,
    visible: false,
    center: new google.maps.LatLng(lat, lon),
    radius: 10000
  });
  map.fitBounds(circle.getBounds());
  var infoWindow = new google.maps.InfoWindow({
    position: marker.getPosition()
  });

  var theCircleButton = "<input type='button' onClick='circleVisible(circle)' class='btn' value='Show Circle'></input>";

  infoWindow.setContent("<div style='text-align:center;'>" + theCircleButton + "</div>");

  google.maps.event.addListener(marker, "click", function() {
    infoWindow.open(map, marker);
  });
}

function circleVisible(circle) {
  circle.setVisible(true);
}

function circleInvisible(circle) {
  circle.setVisible(false);
}

function initMap() {
  // Latitude and Longitude for the centre of Australia
  var centreAusLat = -28.080606;
  var centreAusLon = 133.104225;
  var zoomLevelAus = 4;

  // the options used to create the Google Maps object
  var mapOptions = {
    // roughly the centre of Australia
    center: new google.maps.LatLng(centreAusLat, centreAusLon),

    // zoom level set to see all of Australia
    zoom: zoomLevelAus,

    // show a satellite image based map
    mapTypeId: google.maps.MapTypeId.SATELLITE,
  };

  // create the Google Maps object
  var map = new google.maps.Map(
    document.getElementById('map-canvas'), mapOptions
  );

  fillMap(map, centreAusLat, centreAusLon);
}
html,
body,
#map-canvas {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map-canvas"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap" async defer></script>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...