2017-05-25 29 views
0

我想指定谷歌地圖中的位置,我有JavaScript代碼工作正常,但我知道如何從php變量中指定位置 下面是示例代碼。如何在谷歌地圖中指定位置?

<html> 
<head> 
<title></title> 
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCqgya6hvq6zu3Z2xeT5xEGPPi5pY2ize4&callback=initMap"></script> 
</head> 
<body> 

<?php 
//I want to search this location in google map 
$location="Kigali Rwanda"; 
?> 

<script> 
    var myMap; 
    var myLatlng = new google.maps.LatLng(52.518903284520796,-1.450427753967233); 
    function initialize() { 
     var mapOptions = { 
      zoom: 13, 
      center: myLatlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP , 
      scrollwheel: false 
     } 
     myMap = new google.maps.Map(document.getElementById('map'), mapOptions); 
     var marker = new google.maps.Marker({ 
      position: myLatlng, 
      map: myMap, 
      title: 'Name Of Business', 
      icon: 'http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png' 
     }); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

<div id="map" style="width:500px; height: 500px;"> 

</div> 


</body> 
</html> 

請任何人都可以幫我

回答

1

您可以使用Geocoding service在這種情況下

function initMap() { 
 

 
    //var address = '<?php echo $location ?>'; 
 
    var address = 'Kigali Rwanda'; 
 

 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    zoom: 8 
 
    }); 
 

 
    var geocoder = new google.maps.Geocoder(); 
 

 
    geocoder.geocode({ 
 
     'address': address 
 
    }, 
 
    function(results, status) { 
 
     if (status == google.maps.GeocoderStatus.OK) { 
 
     new google.maps.Marker({ 
 
      position: results[0].geometry.location, 
 
      map: map 
 
     }); 
 
     map.setCenter(results[0].geometry.location); 
 
     } 
 
    }); 
 
}
#map { 
 
    height: 400px; 
 
    width: 100%; 
 
}
<h3>My Google Maps Demo</h3> 
 
<div id="map"></div> 
 
<!-- Replace the value of the key parameter with your own API key. --> 
 
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"> 
 
</script>

+0

謝謝你這麼多深,這就是我想要的。 –

+0

不客氣。 –

0

,你必須使用緯度,液化天然氣specifiy位置。在國家或州名稱的情況下,您應該獲取緯度,位置並在代碼中定義它。在隱藏的輸入字段中插入php變量的值。在運行腳本獲取價值並處理它。