谷歌地图地理编码PHP可以在IE中工作,但不能在Chrome/mobile、Edge或Safari/mobile中工作.
Google map Geocoding PHP works in IE, but not Chrome/ mobile, Edge or Safari/Mobile. I get the error message "unexpected token ?"
我一直在做一些研究,但似乎在这里找不到问题。
<body>
<?php
if($_POST){
// get latitude, longitude and formatted address
$data_arr = geocode($_POST['address']);
// if able to geocode the address
if($data_arr){
$latitude = $data_arr[0];
$longitude = $data_arr[1];
$formatted_address = $data_arr[2];
?>
<!-- google map will be shown here -->
<div id="gmap_canvas">Loading map...</div>
<div id='map-label'>Map shows approximate location.</div>
<!-- JavaScript to show google map -->
<script type="text/javascript" src="http://maps.google.com/maps/api/js"></script>
<script type="text/javascript">
function init_map() {
var myOptions = {
zoom: 14,
center: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(<?php echo $latitude; ?>, <?php echo $longitude; ?>)
});
infowindow = new google.maps.InfoWindow({
content: "<?php echo $formatted_address; ?>"
});
google.maps.event.addListener(marker, "click", function () {
infowindow.open(map, marker);
});
infowindow.open(map, marker);
}
google.maps.event.addDomListener(window, 'load', init_map);
</script>
<?php
// if unable to geocode the address
}else{
echo "No map found.";
}
}
?>
<div id='address-examples'>
<div>Address examples:</div>
<div></div>
<div></div>
</div>
<!-- enter any address -->
<form action="" method="post">
<input type='text' name='address' placeholder='Enter any address here' />
<input type='submit' value='Geocode!' />
</form>
<?php
// function to geocode address, it will return false if unable to geocode address
function geocode($address){
// url encode the address
$address = urlencode($address);
// google map geocode api url
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// response status will be 'OK', if able to geocode given address
if($resp['status']=='OK'){
// get the important data
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
$formatted_address = $resp['results'][0]['formatted_address'];
// verify if data is complete
if($lati && $longi && $formatted_address){
// put the data in the array
$data_arr = array();
array_push(
$data_arr,
$lati,
$longi,
$formatted_address
);
return $data_arr;
}else{
return false;
}
}else{
return false;
}
}
?>
</body>
//在下面的行中,我得到错误消息"意外令牌?"
center: new google.maps.LatLng(<?php echo $latitude; ?>,<?php echo $longitude; ?>),
很抱歉,如果这还不够,这是我第一次使用谷歌地图。
我发现我的代码不是问题所在。我需要更换
script type="text/javascript"src="http://maps.google.com/maps/api/js">
至
script type="text/javascript"src="https://maps.google.com/maps/api/js">
出于某种原因,浏览器需要安全地访问这个src链接,而我在http上没有"s"。一旦我添加了S,它就开始在所有浏览器上运行。
相关文章:
- JSON:查询联接表不工作(Cordova Mobile)
- onClick按钮不工作javascript/php/jquery mobile
- JQuery mobile 1.3.2 : $.mobile.loading 停止在 iOS 上工作
- $.mobile.change页面在多页中无法正常工作
- 在多页$.mobile.changePage(“#test”)中不工作
- 光滑的滑块只在Jquery Mobile中第一次工作
- setInterval没有'使用jquery mobile时无法工作
- JQuery Mobile中的日期选择器不工作
- 在我使用$.mobile.changePage后,onload无法工作
- jQuery mobile-进度条(加载条)在所有元素呈现之前不工作(已停止)
- jQuery Mobile App 的后台工作线程
- Javascript函数在IE中工作,但在FF,Chrome或Mobile Safari中不起作用
- jQuery Mobile加载微调器无法在移动设备上工作
- jQuery Mobile-Accordion中的链接和单选按钮不工作
- 谷歌地图地理编码PHP可以在IE中工作,但不能在Chrome/mobile、Edge或Safari/mobile中工作.
- 可折叠onclick事件中的Jquery Mobile列表没有'动态添加时无法工作
- 如何让锚链接在 Jquery Mobile 中工作
- jQuery Mobile双范围滑块工作,但有缺陷
- jQuery Mobile中的全局变量不再工作
- 使用JQuery mobile创建ListView;我不能正常工作