说,你去谷歌地图并搜索'沃尔玛',然后选择列表中的第一个沃尔玛,你会得到一个URL:
https://www.google.com/maps/place/Walmart+Supercenter/@41.7991677,-70.5882922,10z/data=!4m5!1m2!2m1!1swalmart!3m1!1s0x89e4c6127cc5b685:0x104d337439f46ea9
然后,如果你转到那个URL,它会为你提供一个带有精确定位的有效地图。这(上面)是将存储在我的数据库中的URL,因此我想用来嵌入地图的URL。
当然,我无法使用该网址嵌入iframe,而是必须使用:
https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d380717.11119594605!2d-70.5882922!3d41.7991677!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89e4c6127cc5b685%3A0x104d337439f46ea9!2sWalmart+Supercenter!5e0!3m2!1sen!2sus!4v1445294674664
所以...对于任何可共享的Google地图网址,如何将其转换为可嵌入的网址以用作iframe src
?
我可以使用PHP或客户端使用javascript在服务器端执行此操作。使用正则表达式是可以接受的。
答案 0 :(得分:5)
这可以解决问题:
$matches = [];
preg_match("/@(.*?),(.*?),/",$str,$matches);
$place = $matches[1];
preg_match("/@(.*?),(.*?),/",$str,$matches);
$lat = $matches[1];
$long = $matches[2];
echo '<iframe src="https://www.google.com/maps/embed/v1/place?q='.$place.'¢er='.$lat.','.$long.'&key=AIzaSyAN0om9mFmy1QN6Wf54tXAowK4eT0ZUPrU&zoom=8" frameborder="0"></iframe>';
在第一个@ char之后找到纬度和logitude,您可以使用我编写的正则表达式提取,然后捕获2个组的输出并将其添加到嵌入的URL上。
答案 1 :(得分:1)
不知道你为什么要进行这种转换,但是如果你使用的是javascript客户端,那么你可以使用Google maps v3来获取javascript
然后你将直接获得lat / lng并且你可以存储它们并用于所有未来的呼叫,这似乎更清晰的方法。
请参阅以下代码
<!DOCTYPE html>
<html>
<head>
<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);
}
#pac-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: 300px;
}
#pac-input:focus {
border-color: #4d90fe;
}
.pac-container {
font-family: Roboto;
}
#type-selector {
color: #fff;
background-color: #4d90fe;
padding: 5px 11px 0px 11px;
}
#type-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
</style>
<title>Places Searchbox</title>
<style>
#target {
width: 345px;
}
</style>
</head>
<body>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map"></div>
<script>
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: -33.8688,
lng: 151.2195
},
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
// [START region_getplaces]
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
var marker;
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
console.log('place: '+places[0]);
if(marker!=null)
marker.setMap(null);
var icon = {
url: places[0].icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
marker = new google.maps.Marker({
map: map,
// icon: icon,
title: places[0].name,
position: places[0].geometry.location
});
map.setCenter(places[0].geometry.location);
//save places[0].geometry.location for all future reference
});
// [END region_getplaces]
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initAutocomplete" async defer></script>
</body>
</html>
当然,如果您需要,可以删除搜索列表,默认情况下总是选择第一个结果。
答案 2 :(得分:1)
客户端选项:
$(function() {
var str = 'https://www.google.com/maps/place/Walmart+Supercenter/@41.7991677,-70.5882922,10z/data=!4m5!1m2!2m1!1swalmart!3m1!1s0x89e4c6127cc5b685:0x104d337439f46ea9';
var matches = str.match(/\/@([\d\.,-]+)z\//)[1];
var splits = matches.split(',');
var lat = splits[0];
var long = splits[1];
var zoom = splits[2];
var url = 'http://maps.google.com/maps?q=' + lat + ',' + long + '&z=' + zoom + '&output=embed';
$('#output').html('<iframe style = "width:200px;height:400px" src = ' + url + '></iframe>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='output' style="width:200px;height:400px"></div>