P.s: in this article, you will find a way to obtain the latitude and longitude Based on the city keyword, that is, to obtain the latitude and longitude through the reverse latitude and longitude query interface of the Google map API.
Google map APIIs a powerful map API, many well-known websites are using Google map API, once I also wrote a program based on visitors or even domain names to query latitude and longitude (http://js8.in/mywork/ipsearch ). The link address of this article is: Return The geographic location and address based on the IP address and the geographic latitude and longitude. For other articles on the latitude and longitude, refer to the SQL data of each province, city, county, and city in China.JS Array
The pure IP database is used, and the inverse longitude and latitude of Google Maps are queried ~
In this example, the main method we implement is how PHP obtains the longitude and latitude of the keyword.
Anyone who has used the Google map API knows that Google has an interface for querying against longitude and latitude, for example:
Geocoder =NewGclientgeocoder (); geocoder. getlocations (
'Shandong Qingdao ',
Function($ ){
VaRLalton = $. placemark [0]. Point. Coordinates;
Alert (latlon [0] + "," + latlon [1]);
});
But how should PHP obtain the longitude and latitude?
Today, I saw a WP plug-in without intention. I found the method to obtain the latitude and longitude through the map API's inverse latitude and longitude query interface ~
First, you need to go to http://code.google.com/intl/zh-cn/apis/maps/signup.htmlto apply for an authorized domain name key for the GoogleMap API,
Google's map API's inverse latitude and longitude interface is:
Http://maps.google.com/maps/geo? Q = keywords (for example, Shandong Qingdao) & Key = the applied apikey & sensor = false & Output = xml & OE = utf8
How can I parse the kml data format when the requested data is stored?
We can use XML for parsing. Here I provide a PHP code for parsing. The Code comes from a plug-in of WP.
The first function is xml2array (). The function is used to convert XML into an array for ease of operation.
The $ URL parameter is the XML address of the request, and an array converted from XML is returned.
// From http://us3.php.net/manual/en/function.xml-parse.php
FunctionXml2array ($ URL, $ get_attributes = 1, $ priority = 'tag ')
{
$ Contents = "";
If (! Function_exists ('xml _ parser_create '))
{
Return array ();
}
$ Parser = xml_parser_create ('');
If (! ($ Fp = @ fopen ($ URL, 'rb ')))
{
Return array ();
}
While (! Feof ($ FP ))
{
$ Contents. = fread ($ FP, 8192 );
}
Fclose ($ FP );
Xml_parser_set_option ($ parser, xml_option_target_encoding, "UTF-8 ");
Xml_parser_set_option ($ parser, xml_option_case_folding, 0 );
Xml_parser_set_option ($ parser, xml_option_skip_white, 1 );
Xml_parse_into_struct ($ parser, trim ($ contents), $ xml_values );
Xml_parser_free ($ parser );
If (! $ Xml_values)
Return;// Hmm...
$ Xml_array = array ();
$ Parents = array ();
$ Opened_tags = array ();
$ Arr = array ();
$ Current = & $ xml_array;
$ Repeated_tag_index = array ();
Foreach ($ xml_values as $ data)
{
Unset ($ attributes, $ value );
Extract ($ data );
$ Result = array ();
$ Attributes_data = array ();
If (isset ($ value ))
{
If ($ priority = 'tag ')
$ Result = $ value;
Else
$ Result ['value'] = $ value;
}
If (isset ($ attributes) and $ get_attributes)
{
Foreach ($ attributes as $ ATTR => $ Val)
{
If ($ priority = 'tag ')
$ Attributes_data [$ ATTR] = $ val;
Else
$ Result ['attr'] [$ ATTR] = $ val;// Set all the attributes in a arraycalled 'attr'
}
}
If ($ type = "open ")
{
$ Parent [$ level-1] = & $ current;
If (! Is_array ($ current) or (! In_array ($ tag, array_keys ($ current ))))
{
$ Current [$ tag] = $ result;
If ($ attributes_data)
$ Current [$ tag. '_ ATTR'] = $ attributes_data;
$ Repeated_tag_index [$ tag. '_'. $ level] = 1;
$ Current = & $ current [$ tag];
}
Else
{
If (isset ($ current [$ tag] [0])
{
$ Current [$ tag] [$ repeated_tag_index [$ tag. '_'. $ level] = $ result;
$ Repeated_tag_index [$ tag. '_'. $ level] ++;
}
Else
{
$ Current [$ tag] = array (
$ Current [$ tag],
$ Result
);
$ Repeated_tag_index [$ tag. '_'. $ level] = 2;
If (isset ($ current [$ tag. '_ ATTR'])
{
$ Current [$ tag] ['0 _ ATTR '] = $ current [$ tag.' _ ATTR '];
Unset ($ current [$ tag. '_ ATTR']);
}
}
$ Last_item_index = $ repeated_tag_index [$ tag. '_'. $ level]-1;
$ Current = & $ current [$ tag] [$ last_item_index];
}
}
Elseif ($ type = "complete ")
{
If (! Isset ($ current [$ tag])
{
$ Current [$ tag] = $ result;
$ Repeated_tag_index [$ tag. '_'. $ level] = 1;
If ($ priority = 'tag' and $ attributes_data)
$ Current [$ tag. '_ ATTR'] = $ attributes_data;
}
Else
{
If (isset ($ current [$ tag] [0]) andis_array ($ current [$ tag])
{
$ Current [$ tag] [$ repeated_tag_index [$ tag. '_'. $ level] = $ result;
If ($ priority = 'tag' and $ get_attributes and $ attributes_data)
{
$ Current [$ tag] [$ repeated_tag_index [$ tag. '_'. $ level]. '_ ATTR'] = $ attributes_data;
}
$ Repeated_tag_index [$ tag. '_'. $ level] ++;
}
Else
{
$ Current [$ tag] = array (
$ Current [$ tag],
$ Result
);
$ Repeated_tag_index [$ tag. '_'. $ level] = 1;
If ($ priority = 'tag' and $ get_attributes)
{
If (isset ($ current [$ tag. '_ ATTR'])
{
$ Current [$ tag] ['0 _ ATTR '] = $ current [$ tag.' _ ATTR '];
Unset ($ current [$ tag. '_ ATTR']);
}
If ($ attributes_data)
{
$ Current [$ tag] [$ repeated_tag_index [$ tag. '_'. $ level]. '_ ATTR'] = $ attributes_data;
}
}
$ Repeated_tag_index [$ tag. '_'. $ level] ++;
// 0 and 1 index is already taken
}
}
}
Elseif ($ type = 'close ')
{
$ Current = & $ parent [$ level-1];
}
}
Return ($ xml_array );
}
The second function getpoiandadd ($ address, $ mapkey ):
$ Address is the keyword of the query, and the address is OK. $ mapkey is the Google map API key you applied,
An array is returned, including the longitude and latitude, and the detailed address ~
FunctionGmshc_point ($ address, $ apikey ){
$ Find = array ("\ N","\ R","");
$ Replace = array ("", "", "+ ");
$ Address = str_replace ($ find, $ replace, $ address );
$ Url = 'HTTP: // maps.google.com/maps/geo? Q = '. $ address.' & Key = '. $ apikey.
'& Sensor = false & Output = xml & OE = utf8 ';
$ Response = xml2array ($ URL );// All functions are called for parsing.
$ Coordinates = $ response ['kml'] ['response'] ['placemark'] ['point'] ['coordinates '];
$ Address = $ response ['kml'] ['response'] ['placemark'] ['address'];
If (! Empty ($ coordinates )){
$ Point_array = Split (",", $ coordinates );
$ Point = $ point_array [1]. ",". $ point_array [0];
$ Response = array ('point' => $ point, 'address' => $ address );
Return $ response;
}
}