简体   繁体   English

从Google Map Link提取经度和纬度

[英]Extract Latitude and Longitude from Google Map Link

Supposing I had the following link to a google map: https://www.google.com.gh/maps/place/Niagara+Falls/@43.0828162,-79.0763516,17z/data=!4m15!1m9!4m8!1m0!1m6!1m2!1s0x89d34307412d7ae9:0x29be1d1e689ce35b!2sNiagara+Falls,+NY+14303,+United+States!2m2!1d-79.0741629!2d43.0828162!3m4!1s0x89d34307412d7ae9:0x29be1d1e689ce35b!8m2!3d43.0828162!4d-79.0741629 假设我有以下指向Google地图的链接: https : //www.google.com.gh/maps/place/Niagara+Falls/@43.0828162,-79.0763516,17z/data=!4m15!1m9!4m8!1m0! 1m6!1m2!1s0x89d34307412d7ae9:0x29be1d1e689ce35b!2sNiagara + Falls + NY + 14303,+ United + States!2m2!1d-79.0741629!2d43.0828162!3m4!1s0x89d34307412e7ae9:0x29be1!4d

I would like to extract only the latitude and longitude from the following link. 我只想从以下链接中提取经度和纬度。 I assume I would need to use regular expression to do this but I have no idea how to go about it. 我认为我需要使用正则表达式来执行此操作,但是我不知道该如何处理。

The co-ordinates in this case would be 43.0828162,-79.0763516 在这种情况下,坐标为43.0828162,-79.0763516

I would use a simple RegX like this 我将使用像这样的简单RegX

preg_match('/@(\-?[0-9]+\.[0-9]+),(\-?[0-9]+\.[0-9]+)/', $url, $match );

You can see it here, using your url. 您可以使用您的网址在这里看到它。

https://regex101.com/r/hD7lV0/1 https://regex101.com/r/hD7lV0/1

just for sake of explaining it 只是为了解释它

  • Match @ Literal At sign one time 一次匹配@ Literal At Sign
  • Match - , literal hyphen/minus ? 匹配- ,文字连字符/减号? one or none times 一次或一次
  • Match [0-9] , + one or more times 匹配[0-9]+ 1次或多次
  • Match \\. 匹配\\. literal . 字面的. decimal one time 十进制一次
  • Match [0-9] , + one or more times 匹配[0-9]+ 1次或多次
  • Match , , literal , comma one time etc... 一次匹配,文字,逗号等...

I would do. 我会做。

<?php
    $latLong = split(',',preg_match('/@([0-9\.-,]*?)\//',$url));
    // $latLon = [lat, long] = $latLon[0],$latLom[1]
?>
  • Match @ Symbol (as start of match) 匹配@符号(作为比赛开始)
  • Any Number, Fullstop, minus or comma up-until the next '/' (escaped with '\\') 任意数字,句号,减号或逗号,直到下一个'/'(以'\\'转义)
  • Split it into two parts on the ',' 将其分为两部分,“,”

If needs be you can test it with 如果需要,您可以使用

if (count($latLon) > 2){
    die('Invalid URL Segment found.');
}

If you want to be really short with your regex 如果您想真正使用正则表达式做空

    $latLong = split(',',preg_match('/@(.*?)\//',$url));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM