如何获取当前位置的地址php

时间:2013-05-23 09:42:03

标签: php latitude-longitude google-latitude

通过地址,我可以得到经度和纬度。但如果我不知道地址,我可以通过使用PHP知道我在哪里或我的地址吗?使用此代码,我可以获得CamMob的纬度和长度:

<?php

$address = urlencode("#18C, Street 584, Toul Kork, Phnom Penh, Cambodia");
//here is the google api url
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";
//get the content from the api using file_get_contents
$getmap = file_get_contents($url);
//the result is in json format. To decode it use json_decode
$googlemap = json_decode($getmap);
//get the latitute, longitude from the json result by doing a for loop

foreach($googlemap->results as $res){
$address = $res->geometry;
$latlng = $address->location;
$formattedaddress = $res->formatted_address;


echo "Latitude: ". $latlng->lat ."<br />". "Longitude:". $latlng->lng;

}

?>

但如果我不知道我的地址,怎么去?

3 个答案:

答案 0 :(得分:0)

尝试使用此功能。也许它会有所帮助。

的var_dump($ _ SERVER)

答案 1 :(得分:0)

如果您没有输入字符串,即在您的情况下它是地址,浏览器中唯一的其他方法是使用HTML5地理位置。根据我的经验,它仍然不那么准确,而且仍然越来越好。

点击此链接:http://html5demos.com/geo

从HTML5地理位置查找器中获取位置并将其放在$ address字段中。

然后你应该能够解决你的问题。

希望这会有所帮助。

答案 2 :(得分:0)

您仍然可以使用IP地址$ _SERVER'http_host'找到它并使用此IP地址使用freegeoip.net api服务查找当前位置