Home > Backend Development > PHP Tutorial > PHP code to get the latitude and longitude of an address

PHP code to get the latitude and longitude of an address

WBOY
Release: 2016-07-25 09:00:00
Original
1251 people have browsed it
How do you get the longitude and latitude of a specific location using the Google Maps API? This article provides you with a function that takes a certain address as a parameter and returns an array containing longitude and latitude data. Friends in need can refer to it.

The code is as follows:

<?php
/**
 * 获取某地址的经纬度
 * by http://bbs.it-home.org
*/
function getLatLong($address){ 
    if (!is_string($address))die("All Addresses must be passed as a string"); 
    $_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address)); 
    $_result = false; 
    if($_result = file_get_contents($_url)) { 
        if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false; 
        preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match); 
        $_coords['lat'] = $_match[1]; 
        $_coords['long'] = $_match[2]; 
    } 
    return $_coords; 
}
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template