< HTML > < head > < Meta HTTP-equiv = "Content-Type" Content = "text / HTML; charset = UTF-8" /> < title > Auto Obtain current city visitors </ title > < Script of the type = "text / JavaScript" src = "http://api.map.baidu.com/api?v=2.0&ak= your API key" > </ Script > </ head > < body > < div the above mentioned id = "bdMapBox"style= "Run the display: none;" > </ div > < Script of the type = "text / JavaScript" > // Baidu Maps API function var = new new BMap.Map the Map ( "bdMapBox"); var nowCity = new new BMap.LocalCity (); nowCity.get (bdGetPosition); function bdGetPosition (Result) { var cityName = result.name; // current city name / * custom code * / atCity.innerHTML = cityName; / * custom code * / } </ Script > < the p- > your current city as: < span the above mentioned id = "atCity" ></span
></p> </body> </html>
The first is recommended, because Sohu API without registering, so.
The following is a modification of the method of Sohu API:
1. After configured API, can not work at the site http, but https can
Solution: The API is written in https to http.
2. Once you've configured above, click on the web page only shows their ip address (and pjax related)
Solution: The document.write changed document.getElementById