codeIgniter谷歌地图api V3坐标

时间:2014-04-07 20:31:52

标签: codeigniter google-maps-api-3 geolocation

我使用地图从演示到位置人geolocation map我的问题是我想将坐标保存到数据库但我不知道怎么做?

1 个答案:

答案 0 :(得分:0)

使用google和curl对位置进行编码...以下是我编写的示例函数,现已在生产网站上使用:

//$loc can be any kind of location from 555 street name, city, st, zip to just city, state, etc.

$loc = 'Austin, TX';    
function get_geo_loc($loc) {
          $string = str_replace (" ", "+", urlencode($loc));
           $details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false";

           $ch = curl_init();
           curl_setopt($ch, CURLOPT_URL, $details_url);
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
           $response = json_decode(curl_exec($ch), true);

           $lat = $response['results'][0]['geometry']['location']['lat'];
           $lng = $response['results'][0]['geometry']['location']['lng'];

          return array(
             'lat' => $lat,
             'lng' => $lng
          );
       }