我有一个谷歌地图链接,如下所示:
如何从PHP或Javascript中提取坐标以获取数组:
{47.620506, -122.349277}
答案 0 :(得分:2)
如果您没有使用api(这是不可取的),您可以将页面放入一个字符串,并从内部提取长/ lat ...它在页面上多次给出作为参数echo $myArray['id']; // 1
...我对正则表达式不太了解,所以我将使用ll
strstr
答案 1 :(得分:1)
这是我目前在网页上使用的代码。它也很容易做到。
<div id="map-canvas">
<script>
function initialize() {
var myLatlng;
var mapOptions = {
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
//User Location
/* This is the cords */ myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//So if you want them individually just use position.coords.l...
//Please feel free to ask questions
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</div>
答案 2 :(得分:0)
如果您使用php,那么使用Google将识别的任何地址查询地图api非常容易:
$address = 'Space+Needle';
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response);
$ltlg = array($result->results[0]->geometry->location->lat, $result->results[0]->geometry->location->lng);
希望这有帮助
答案 3 :(得分:0)
试试这个:
public function getCoordinatesAttribute() {
$url = "https://www.google.com/maps/place/Space+Needle/@47.620506,-122.349277,17z/data=!4m6!1m3!3m2!1s0x5490151f4ed5b7f9:0xdb2ba8689ed0920d!2sSpace+Needle!3m1!1s0x5490151f4ed5b7f9:0xdb2ba8689ed0920d";
$url_coordinates_position = strpos($url, '@')+1;
$coordinates = [];
if ($url_coordinates_position != false) {
$coordinates_string = substr($url, $url_coordinates_position);
$coordinates_array = explode(',', $coordinates_string);
if (count($coordinates_array) >= 2) {
$longitude = $coordinates_array[0];
$latitude = $coordinates_array[1];
$coordinates = [
"longitude" => $longitude,
"latitude" => $latitude
];
}
return $coordinates;
}
return $coordinates;
}
就我而言,我必须确保链接是谷歌地图位置 URL,因为这完全取决于 网址中的字符“@”。