Changing from Google maps api v2 to v3

空扰寡人 提交于 2019-12-06 16:11:36

As you are storing the coordinates in database it would be best to geocode when you insert new record. ie

require("dbinfo.php");//Your database parameters
//Connect to database
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
    //Prepare query
    $name = "%".$company."%";//Wildcard for PDO paramerter
    $countSql = "SELECT COUNT(*) FROM markers WHERE `name` LIKE ?"; 
    $countStmt = $dbh->prepare($countSql);
    // Assign parameter
    $countStmt->bindParam(1,$name);
    //Execute query
    $countStmt->execute();  
    // check the row count  
    if ($countStmt->fetchColumn() == 0) { #1 EDIT changed >0 to ==0
        echo "No row matched the query."; //EDIT  From Row
        $q =$address.','.$city.','.$post_code.',UK'; 
        echo "\n";
        $base_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
        $request_url = $base_url.urlencode($q)."&sensor=false";
        $xml = simplexml_load_file($request_url) or die("url not loading");
        if($xml->status=="OK"){#2
            // Successful geocode
            $lat = $xml->result->geometry->location->lat;
            $lng = $xml->result->geometry->location->lng; 
            $insertSql ="INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`, `link`) VALUES (?,?,?,?,?,?)";
            $insertStmt = $dbh->prepare($insertSql);
            // Assign parameter
            $insertStmt->bindParam(1,$company);
            $insertStmt->bindParam(2,$address); 
            $insertStmt->bindParam(3,$lat);
            $insertStmt->bindParam(4,$lng);
            $insertStmt->bindParam(5,$type);
            $insertStmt->bindParam(6,$link);
            //Execute query
            $insertStmt->execute();
        } #2
        else{
            "No rows inserted."; 
        }#2
    } #1
    else {#1
    echo "Rows matched the query."; //EDIT From No row
    } #1 
}// End try 


catch(PDOException $e) {
    echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing 
    file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", myFile.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
 }

I have converted your code to PDO as it is advisable to stop using mysql_functions as these a deprecated.

I have left you to implement how you will deal with geocoding not returning coordinates. You can also check and deal with the following status codes

OVER_QUERY_LIMIT

ZERO_RESULTS

REQUEST_DENIED

INVALID_REQUEST

See pastebin For status code implementation

Going by our earlier comments, there are just a couple of changes required that should get you back on your feet.

These are, changing the address for v3 geocoding

define("MAPS_HOST", "maps.googleapis.com");
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?";
$request_url = $base_url . "address=" . urlencode($address) . "&sensor=false";

And secondly changing the path in the returned xml file for setting lat/long

$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];

Can be completely replaced with

$lat = $xml->result->geometry->location->lat;
$lng = $xml->result->geometry->location->lng;

The next piece about stopping it from going over geocoding limits. What you need to do is set a simple check before you run through the geocoding.

while ($row = @mysql_fetch_assoc($result)) {
    if (!($row['lat'] * 1)) {// add this line
        $geocode_pending = true;
            while ($geocode_pending){
                //do geocoding stuff
            }
        }
    }// add this close
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!