0

GMaps Elevation Service には Google Developers の GMaps Code を使用しています。地図をクリックすると、情報ウィンドウが自動的に開きます。ここで、マーカー オブジェクトを統合しようとしました。私の目的は、標高情報を運ぶマップ上のマーカーを「フリーズ」することです。ただし、まず段階的に、マップをクリックしてもマーカーが表示されません。ヒントをいただければ幸いです。ありがとう!

コードは次のとおりです。

 var elevator;
 var map;

var freiburg = new google.maps.LatLng(47.9971865, 7.8537668);
var marker_options = {
         map : map,
         icon : {  url : 'http://maps.google.com/mapfiles/marker.png',
                   size : new google.maps.Size(32,32),
                   anchor : new google.maps.Point(0,0),
                   origin: new google.maps.Point(0,32)

         }
  };

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

 //new marker object

var marker = new google.maps.Marker(marker_options);



 function initialize() {
 var mapOptions = {
 zoom: 13,
 center: freiburg,
 mapTypeId: 'terrain'
 }

 map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);


 // Create an ElevationService
 elevator = new google.maps.ElevationService();

 // Add a listener for the click event and call getElevation on that location
  google.maps.event.addListener(map, 'click', getElevation);
   }

function getElevation(event) {

 var locations = [];

 // Retrieve the clicked location and push it on the array
 var clickedLocation = event.latLng;
 locations.push(clickedLocation);

  // Create a LocationElevationRequest object using the array's one value
  var positionalRequest = {
  'locations': locations
   }

  // Initiate the location request
  elevator.getElevationForLocations(positionalRequest, function(results, status) {
  if (status == google.maps.ElevationStatus.OK) {

  // Retrieve the first result
  if (results[0]) {

    //position Marker
    marker.setPosition(clickedLocation);

    // Open an info window indicating the elevation at the clicked position
    infowindow.setContent(Math.round(results[0].elevation) + ' m. N. N.');
    infowindow.setPosition(clickedLocation);
    infowindow.open(map);
  } else {
    alert('No results found');
  }
  } 
   else {
     alert('Elevation service failed due to: ' + status);
    }
     });
         }
4

1 に答える 1