如何在不支持html5移动设备的浏览器中确定位置



我有一个html5网页,根据移动设备的地理/位置显示特定的广告。

如何从(旧的)浏览器内部确定不支持html5(旧的诺基亚和黑莓)的手机上的位置?

您必须基于IP地址在服务器端执行此操作,使用诸如Geo::IP之类的库。它的准确性要低得多(尤其是在移动设备上),但如果浏览器不给你一个位置,你就没有办法猜测它。

我假设你可以在你的网页中使用Javascript。你可以使用geopotion .js的回退技术。也有特定于设备的解决方案可用。

所以你可以从检测对HTML5的支持开始(而不是假设)

if (Modernizr.geolocation) {
// let's find out where you are!
} else {
// no native geolocation support available :(
// try geoPosition.js or another third-party solution
}

您可以从cdn资源中包含Modernizr库<script src="modernizr.min.js"></script>现在使用地理位置你可以解决这个问题。下面是来自github的示例在标题部分

<script src="js/geoPosition.js" type="text/javascript" charset="utf-8"></script>

那么就这样命名

<script type="text/javascript">
if(geoPosition.init()){  // Geolocation Initialisation
        geoPosition.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});
}else{
        // You cannot use Geolocation in this device
}
geoPositionSimulator.init(); 
// p : geolocation object
function success_callback(p){
    // p.latitude : latitude value
    // p.longitude : longitude value
}
function error_callback(p){
    // p.message : error message
}

您可以参考以下可能对您有所帮助的资源

检测地理位置:http://diveintohtml5.info/detect.html#geolocation使用地理位置:github.com/estebanav/javascript-mobile-desktop-geolocation黑莓专用资源:tonybunce.com/2008/05/08/Blackberry-Browser-Amp-GPS.aspx

希望能有所帮助

最新更新