Created
November 12, 2018 11:59
-
-
Save trdev7/46a96d83416efa4164a992ca8269d686 to your computer and use it in GitHub Desktop.
This is php script that get longitude and latitude via google map api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set( 'max_execution_time', 33600 ); | |
$m = new Mongo( "mongodb://nosavn.net:27018" ); | |
// print_r($m->getConnections()); | |
// var_dump($m); | |
$db = $m->selectDB( 'Nosa' ); | |
$db->authenticate( "nosavnnet", "nsvn6688" ); | |
//connect collection | |
$collectionWard = new MongoCollection( $db, 'Ward' ); | |
$collectionDistrict = new MongoCollection( $db, 'District' ); | |
$collectionProvince = new MongoCollection( $db, 'Provincial' ); | |
//end connect collection | |
$longitude = 0; | |
$latitude = 0; | |
function cal_LatAndLong( $ward="", $district="", $province="" ) { | |
global $longitude, $latitude; | |
$address = $ward; | |
if( $district != "" ) $address .= ", " . $district; | |
if( $province != "" ) $address .= ", " . $province; | |
// echo $address; | |
// 'Phúc Xá, Ba Đình, Hanoi, Vietnam'; | |
//$url = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false"); | |
$url = file_get_contents( "http://maps.google.com/maps/api/geocode/json?address=" . urlencode( $address ) . "&sensor=false&key=AIzaSyAP-4ax535sASPvI37YvJcm9h4TdKjcZ6s" ); | |
$response = json_decode($url); | |
if ($response->status == 'OK') { | |
$latitude = $response->results[0]->geometry->location->lat; | |
$longitude = $response->results[0]->geometry->location->lng; | |
// echo 'Latitude: ' . $latitude; | |
// echo '<br />'; | |
// echo 'Longitude: ' . $longitude; | |
} else { | |
$latitude=null; | |
$longitude=null; | |
} | |
} | |
//$cursor = $collectionWard->find(); | |
// $cursor = $collectionDistrict->find(); | |
$cursor = $collectionWard->find( array( "_id" => "05494" ) ); | |
// $cursor = $collectionDistrict->find(array("_id"=>"001")); | |
$count__=0; | |
foreach( $cursor as $doc ) { | |
$count__++; | |
$id = ( int )$doc[ "_id" ]; | |
//if($id<=5494 || $id>10000) continue; | |
// $provinceData = $collectionProvince->findone(array("_id"=>$doc["IdProvince"])); | |
$districtData = $collectionDistrict->findone( array( "_id" => $doc[ "IdDistrict" ] ) ); | |
$provinceData = $collectionProvince->findone( array( "_id" => $districtData[ "IdProvince" ] ) ); | |
$wardname = $doc[ "Name" ]; | |
$districtName = $districtData[ "Name" ]; | |
$provinceName = $provinceData[ "Name" ]; | |
// $districtName = $doc["Name"]; | |
// $provinceName = $provinceData["Name"]; | |
$longitude = null; | |
$latitude = null; | |
// while($longitude==null || $longitude==null) | |
// cal_LatAndLong("",$districtName,$provinceName); | |
while( $longitude == null || $longitude == null ) { | |
echo $count__ . ") " . $doc[ "_id" ] . " " . $doc[ "Name" ] . " " . $latitude . " " . $longitude . " Impossible to get</br>"; | |
ob_flush( ); | |
flush( ); | |
$errcount++; | |
if ( $errcount > 5 ) die( "PROBABLY LIMIT REACHED" ); | |
cal_LatAndLong( $wardname, $districtName, $provinceName ); | |
} | |
// if($latitude==null || $longitude==null) | |
$collectionWard->update( array ( "_id" => $doc[ "_id" ] ), | |
array ( '$set' => array( "Long" => $longitude, "Lati" => $latitude ) ) ); | |
// var_dump($doc); | |
echo $count__ . ") " . $doc[ "_id" ] . " " . $doc[ "Name" ] . " " . $latitude . " " . $longitude . "</br>"; | |
ob_flush( ); | |
flush( ); | |
} | |
// echo date('h:i:s'); | |
// $cursorone = $collectionWard->findone(array("_id"=>"00001")); | |
// var_dump($cursorone); | |
// var_dump($cursorone); | |
// cal_latandlong("Phúc Xá","Ba Đình","Hanoi"); | |
// echo $latitude; | |
// echo $longitude; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment