2015-11-21 2 views
-1

Привет, я являюсь yiibie, и я пытаюсь реализовать Place Autocomplete and Directions(google map Api), для этого я получил карту Google Api и также включил Google Maps Directions API, Google Maps JavaScript API, Google Places API Web Service. Когда я реализую данный код в файле моего вида (который я скопировал здесь: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-directions) и запустил мой проект, только небольшая часть карты появится на моем экране в течение нескольких секунд, а затем исчезнет. В чем может быть проблема, вот код моего файла просмотра.Yii: google map api (Место Autocomplete и Directions) не работает

<?php 
/* @var $this NgoController */ 
/* @var $model Ngo */ 
/* @var $form BSActiveForm */ 
?> 

<?php $form=$this->beginWidget('bootstrap.widgets.BsActiveForm', array(
    'id'=>'ngo-form', 
    // Please note: When you enable ajax validation, make sure the corresponding 
    // controller action is handling ajax validation correctly. 
    // There is a call to performAjaxValidation() commented in generated controller code. 
    // See class documentation of CActiveForm for details on this. 
    'enableAjaxValidation'=>false, 
)); ?> 

    <p class="help-block">Fields with <span class="required">*</span> are required.</p> 

    <?php echo $form->errorSummary($model); ?> 

    <?php echo $form->textFieldControlGroup($model,'ngo_name',array('maxlength'=>45)); ?> 
    <?php echo $form->textFieldControlGroup($model,'email',array('maxlength'=>45)); ?> 
    <?php echo $form->textFieldControlGroup($model,'address',array('maxlength'=>45)); ?> 
    <?php echo $form->textFieldControlGroup($model,'image',array('maxlength'=>45)); ?> 

    <!--code for map starts here--> 


<!DOCTYPE html> 
<html> 
    <head> 
    <title>Place Autocomplete</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
     #map { 
     height: 100%; 
     } 
.controls { 
    margin-top: 10px; 
    border: 1px solid transparent; 
    border-radius: 2px 0 0 2px; 
    box-sizing: border-box; 
    -moz-box-sizing: border-box; 
    height: 32px; 
    outline: none; 
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); 
} 

#origin-input, 
#destination-input { 
    background-color: #fff; 
    font-family: Roboto; 
    font-size: 15px; 
    font-weight: 300; 
    margin-left: 12px; 
    padding: 0 11px 0 13px; 
    text-overflow: ellipsis; 
    width: 200px; 
} 

#origin-input:focus, 
#destination-input:focus { 
    border-color: #4d90fe; 
} 

#mode-selector { 
    color: #fff; 
    background-color: #4d90fe; 
    margin-left: 12px; 
    padding: 5px 11px 0px 11px; 
} 

#mode-selector label { 
    font-family: Roboto; 
    font-size: 13px; 
    font-weight: 300; 
} 


    </style> 
    </head> 
    <body> 
    <input id="origin-input" class="controls" type="text" 
     placeholder="Enter an origin location"> 

    <input id="destination-input" class="controls" type="text" 
     placeholder="Enter a destination location"> 

    <div id="mode-selector" class="controls"> 
     <input type="radio" name="type" id="changemode-walking" checked="checked"> 
     <label for="changemode-walking">Walking</label> 

     <input type="radio" name="type" id="changemode-transit"> 
     <label for="changemode-transit">Transit</label> 

     <input type="radio" name="type" id="changemode-driving"> 
     <label for="changemode-driving">Driving</label> 
    </div> 

    <div id="map"></div> 

    <script> 
function initMap() { 
    var origin_place_id = null; 
    var destination_place_id = null; 
    var travel_mode = google.maps.TravelMode.WALKING; 
    var map = new google.maps.Map(document.getElementById('map'), { 
    mapTypeControl: false, 
    center: {lat: -33.8688, lng: 151.2195}, 
    zoom: 13 
    }); 
    var directionsService = new google.maps.DirectionsService; 
    var directionsDisplay = new google.maps.DirectionsRenderer; 
    directionsDisplay.setMap(map); 

    var origin_input = document.getElementById('origin-input'); 
    var destination_input = document.getElementById('destination-input'); 
    var modes = document.getElementById('mode-selector'); 

    map.controls[google.maps.ControlPosition.TOP_LEFT].push(origin_input); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(destination_input); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(modes); 

    var origin_autocomplete = new google.maps.places.Autocomplete(origin_input); 
    origin_autocomplete.bindTo('bounds', map); 
    var destination_autocomplete = 
     new google.maps.places.Autocomplete(destination_input); 
    destination_autocomplete.bindTo('bounds', map); 

    // Sets a listener on a radio button to change the filter type on Places 
    // Autocomplete. 
    function setupClickListener(id, mode) { 
    var radioButton = document.getElementById(id); 
    radioButton.addEventListener('click', function() { 
     travel_mode = mode; 
    }); 
    } 
    setupClickListener('changemode-walking', google.maps.TravelMode.WALKING); 
    setupClickListener('changemode-transit', google.maps.TravelMode.TRANSIT); 
    setupClickListener('changemode-driving', google.maps.TravelMode.DRIVING); 

    function expandViewportToFitPlace(map, place) { 
    if (place.geometry.viewport) { 
     map.fitBounds(place.geometry.viewport); 
    } else { 
     map.setCenter(place.geometry.location); 
     map.setZoom(17); 
    } 
    } 

    origin_autocomplete.addListener('place_changed', function() { 
    var place = origin_autocomplete.getPlace(); 
    if (!place.geometry) { 
     window.alert("Autocomplete's returned place contains no geometry"); 
     return; 
    } 
    expandViewportToFitPlace(map, place); 

    // If the place has a geometry, store its place ID and route if we have 
    // the other place ID 
    origin_place_id = place.place_id; 
    route(origin_place_id, destination_place_id, travel_mode, 
      directionsService, directionsDisplay); 
    }); 

    destination_autocomplete.addListener('place_changed', function() { 
    var place = destination_autocomplete.getPlace(); 
    if (!place.geometry) { 
     window.alert("Autocomplete's returned place contains no geometry"); 
     return; 
    } 
    expandViewportToFitPlace(map, place); 

    // If the place has a geometry, store its place ID and route if we have 
    // the other place ID 
    destination_place_id = place.place_id; 
    route(origin_place_id, destination_place_id, travel_mode, 
      directionsService, directionsDisplay); 
    }); 

    function route(origin_place_id, destination_place_id, travel_mode, 
       directionsService, directionsDisplay) { 
    if (!origin_place_id || !destination_place_id) { 
     return; 
    } 
    directionsService.route({ 
     origin: {'placeId': origin_place_id}, 
     destination: {'placeId': destination_place_id}, 
     travelMode: travel_mode 
    }, function(response, status) { 
     if (status === google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
     } else { 
     window.alert('Directions request failed due to ' + status); 
     } 
    }); 
    } 
} 

    </script> 
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAtvEXUyjEkGlAVhwVm9Nr-hCrW2L2MkmU&signed_in=true&libraries=places&callback=initMap" 
     async defer></script> 
    </body> 
</html> 



    <?php echo BsHtml::submitButton('Submit', array('color' => BsHtml::BUTTON_COLOR_PRIMARY)); ?> 

<?php $this->endWidget(); ?> 

ответ

0

HTML, одна работа отлично (карта является шоу), но ваш PHP код cointain теги вне HTML. Попробуйте переместить эту часть кода внутри секции тела, как этот, например:

<!DOCTYPE html> 
    <html> 
     <head> 
     <title>Place Autocomplete</title> 
     <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
     <meta charset="utf-8"> 
     <style> 
      html, body { 
      height: 100%; 
      margin: 0; 
      padding: 0; 
      } 
      #map { 
      height: 100%; 
      } 
    .controls { 
     margin-top: 10px; 
     border: 1px solid transparent; 
     border-radius: 2px 0 0 2px; 
     box-sizing: border-box; 
     -moz-box-sizing: border-box; 
     height: 32px; 
     outline: none; 
     box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); 
    } 

    #origin-input, 
    #destination-input { 
     background-color: #fff; 
     font-family: Roboto; 
     font-size: 15px; 
     font-weight: 300; 
     margin-left: 12px; 
     padding: 0 11px 0 13px; 
     text-overflow: ellipsis; 
     width: 200px; 
    } 

    #origin-input:focus, 
    #destination-input:focus { 
     border-color: #4d90fe; 
    } 

    #mode-selector { 
     color: #fff; 
     background-color: #4d90fe; 
     margin-left: 12px; 
     padding: 5px 11px 0px 11px; 
    } 

    #mode-selector label { 
     font-family: Roboto; 
     font-size: 13px; 
     font-weight: 300; 
    } 


     </style> 
     </head> 
     <body> 

    <?php $form=$this->beginWidget('bootstrap.widgets.BsActiveForm', array(
     'id'=>'ngo-form', 
     // Please note: When you enable ajax validation, make sure the corresponding 
     // controller action is handling ajax validation correctly. 
     // There is a call to performAjaxValidation() commented in generated controller code. 
     // See class documentation of CActiveForm for details on this. 
     'enableAjaxValidation'=>false, 
    )); ?> 

     <p class="help-block">Fields with <span class="required">*</span> are required.</p> 

     <?php echo $form->errorSummary($model); ?> 

     <?php echo $form->textFieldControlGroup($model,'ngo_name',array('maxlength'=>45)); ?> 
     <?php echo $form->textFieldControlGroup($model,'email',array('maxlength'=>45)); ?> 
     <?php echo $form->textFieldControlGroup($model,'address',array('maxlength'=>45)); ?> 
     <?php echo $form->textFieldControlGroup($model,'image',array('maxlength'=>45)); ?> 

    <?php echo BsHtml::submitButton('Submit', array('color' => BsHtml::BUTTON_COLOR_PRIMARY)); ?> 

    <?php $this->endWidget(); ?> 

     <input id="origin-input" class="controls" type="text" 
      placeholder="Enter an origin location"> 

     <input id="destination-input" class="controls" type="text" 
      placeholder="Enter a destination location"> 

     <div id="mode-selector" class="controls"> 
      <input type="radio" name="type" id="changemode-walking" checked="checked"> 
      <label for="changemode-walking">Walking</label> 

      <input type="radio" name="type" id="changemode-transit"> 
      <label for="changemode-transit">Transit</label> 

      <input type="radio" name="type" id="changemode-driving"> 
      <label for="changemode-driving">Driving</label> 
     </div> 

     <div id="map"></div> 

     <script> 
    function initMap() { 
     var origin_place_id = null; 
     var destination_place_id = null; 
     var travel_mode = google.maps.TravelMode.WALKING; 
     var map = new google.maps.Map(document.getElementById('map'), { 
     mapTypeControl: false, 
     center: {lat: -33.8688, lng: 151.2195}, 
     zoom: 13 
     }); 
     var directionsService = new google.maps.DirectionsService; 
     var directionsDisplay = new google.maps.DirectionsRenderer; 
     directionsDisplay.setMap(map); 

     var origin_input = document.getElementById('origin-input'); 
     var destination_input = document.getElementById('destination-input'); 
     var modes = document.getElementById('mode-selector'); 

     map.controls[google.maps.ControlPosition.TOP_LEFT].push(origin_input); 
     map.controls[google.maps.ControlPosition.TOP_LEFT].push(destination_input); 
     map.controls[google.maps.ControlPosition.TOP_LEFT].push(modes); 

     var origin_autocomplete = new google.maps.places.Autocomplete(origin_input); 
     origin_autocomplete.bindTo('bounds', map); 
     var destination_autocomplete = 
      new google.maps.places.Autocomplete(destination_input); 
     destination_autocomplete.bindTo('bounds', map); 

     // Sets a listener on a radio button to change the filter type on Places 
     // Autocomplete. 
     function setupClickListener(id, mode) { 
     var radioButton = document.getElementById(id); 
     radioButton.addEventListener('click', function() { 
      travel_mode = mode; 
     }); 
     } 
     setupClickListener('changemode-walking', google.maps.TravelMode.WALKING); 
     setupClickListener('changemode-transit', google.maps.TravelMode.TRANSIT); 
     setupClickListener('changemode-driving', google.maps.TravelMode.DRIVING); 

     function expandViewportToFitPlace(map, place) { 
     if (place.geometry.viewport) { 
      map.fitBounds(place.geometry.viewport); 
     } else { 
      map.setCenter(place.geometry.location); 
      map.setZoom(17); 
     } 
     } 

     origin_autocomplete.addListener('place_changed', function() { 
     var place = origin_autocomplete.getPlace(); 
     if (!place.geometry) { 
      window.alert("Autocomplete's returned place contains no geometry"); 
      return; 
     } 
     expandViewportToFitPlace(map, place); 

     // If the place has a geometry, store its place ID and route if we have 
     // the other place ID 
     origin_place_id = place.place_id; 
     route(origin_place_id, destination_place_id, travel_mode, 
       directionsService, directionsDisplay); 
     }); 

     destination_autocomplete.addListener('place_changed', function() { 
     var place = destination_autocomplete.getPlace(); 
     if (!place.geometry) { 
      window.alert("Autocomplete's returned place contains no geometry"); 
      return; 
     } 
     expandViewportToFitPlace(map, place); 

     // If the place has a geometry, store its place ID and route if we have 
     // the other place ID 
     destination_place_id = place.place_id; 
     route(origin_place_id, destination_place_id, travel_mode, 
       directionsService, directionsDisplay); 
     }); 

     function route(origin_place_id, destination_place_id, travel_mode, 
        directionsService, directionsDisplay) { 
     if (!origin_place_id || !destination_place_id) { 
      return; 
     } 
     directionsService.route({ 
      origin: {'placeId': origin_place_id}, 
      destination: {'placeId': destination_place_id}, 
      travelMode: travel_mode 
     }, function(response, status) { 
      if (status === google.maps.DirectionsStatus.OK) { 
      directionsDisplay.setDirections(response); 
      } else { 
      window.alert('Directions request failed due to ' + status); 
      } 
     }); 
     } 
    } 

     </script> 
     <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAtvEXUyjEkGlAVhwVm9Nr-hCrW2L2MkmU&signed_in=true&libraries=places&callback=initMap" 
      async defer></script> 
     </body> 
    </html> 
+0

до сих пор не работает, я также HTML, в одиночку, но это дает тот же результат. –

+0

Я пробовал только html, и карты показываются правильно. для остальных я не могу проверить. – scaisEdge

+0

ОК, я снова посмотрю на это, спасибо за помощь .. !! –

Смежные вопросы