我正在研究google maps api,我想将地方数据存储在我的sql server中,我在xml文件中获取数据,从下面的代码我可以存储以下数据(name,place_id,visinity)但是除了它之外我想存储(lat,lng)..
$url ="https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=22.722822,75.887055&radius=2000&type=pharmacy&keyword=medical&key=*************************";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec ($ch);
curl_close($ch);
$xml = simplexml_load_string($data); foreach ($xml -> result as $row) {
$name = $row -> name;
$place_id= $row -> place_id;
$vicinity= $row -> vicinity;
$sql = "INSERT INTO google_coordinate (name,address,place_id) VALUES ('$name','$vicinity','$place_id')";
$result = mysql_query($sql);
if(!$result){
echo 'error';
}else{
echo "success";
}
}
答案 0 :(得分:0)
只需按照这样的数据结构
foreach ($xml->result as $row) {
$name = $row->name;
$place_id = $row->place_id;
$vicinity = $row->vicinity;
$lat = $row->geometry->location->lat;
$lon = $row->geometry->location->lon;
$sql = "INSERT INTO google_coordinate
(name,address,place_id,lat,lon)
VALUES ('$name','$vicinity','$place_id', '$lat', '$lon')";
$result = mysql_query($sql);
if(!$result){
echo 'error';
}else{
echo "success";
}
}
答案 1 :(得分:0)
可以这样做
$lat = $row->geometry->location->lat;
$lng = $row->geometry->location->lng;
$sql = "INSERT INTO google_coordinate (name, address, lat, lng, place_id) VALUES ('$name', '$vicinity', '$lat', '$lng', '$place_id')";