2013-04-20 1 views
0

Я пытаюсь добавить карту Google на свою веб-страницу с помощью asp.net. Используя код, приведенный ниже карты Google, отображается в течение нескольких миллисекунд, а затем исчезает. Я проверил элементы на консоли и отобразилось следующее сообщение: «Ошибка типа« Неподчиненная ошибка: невозможно прочитать свойство »« null ». Пожалуйста, помогите мне найти решение этой проблемы.add google map "Uncaught Type Error: Невозможно прочитать свойство 'style' of null"

<script type="text/javascript"> 
      var directionsDisplay; 
      var directionsService = new google.maps.DirectionsService(); 

      function initialize() { 
       directionsDisplay = new google.maps.DirectionsRenderer(); 
       var mapOptions = { 
        zoom: 7, 
        mapTypeId: google.maps.MapTypeId.ROADMAP, 
        center: new google.maps.LatLng(41.850033, -87.6500523) 
       }; 
       var map = new google.maps.Map(document.getElementById('map-canvas'), 
      mapOptions); 
       directionsDisplay.setMap(map); 
       directionsDisplay.setPanel(document.getElementById('directions-panel')); 

       var control = document.getElementById('control'); 
       control.style.display = 'block'; 

    Uncaught TypeError: Cannot read property 'style' of null 




       map.controls[google.maps.ControlPosition.TOP_CENTER].push(control); 
      } 

      function calcRoute() { 
       var start = document.getElementById('start').value; 
       var end = document.getElementById('end').value; 
       var request = { 
        origin: start, 
        destination: end, 
        travelMode: google.maps.TravelMode.DRIVING 
       }; 
       directionsService.route(request, function (response, status) { 
        if (status == google.maps.DirectionsStatus.OK) { 
         directionsDisplay.setDirections(response); 
        } 
       }); 
      } 

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

     </script> 
+0

Разве это не очевидно? Он говорит, что переменная 'control' равна нулю –

ответ

0

Если элемент управления не существует в странице, вернитесь функции if (control===null) return;

0

элемента с идентификатором control отсутствует. После строки:

var control = document.getElementById('control'); 

переменная control получил значение null и из-за того, что в следующей строке

control.style.display = 'block'; 

произвел ошибки:

Uncaught TypeError: Cannot read property 'style' of null 

Чтобы исправить это, вы должны добавить новый элемент к вашему страница с идентификатором control, например:

<div id="control"></div> 

Если вы хотите добавить пользовательские элементы управления в Google Map, проверьте документы Controls.