我们在 Apple Store 进行了一些测试,发现我们的地理定位代码(获取纬度和经度)不适用于 iPhone、iPod Touch 或 iPad。我确实必须添加额外的代码才能让它在 MacBook / iMac 上工作。
任何人都知道代码来获得在 iPod Touch / iPhone / iPad 设备上肯定有效的纬度/经度?
代码:
// needed for iPhone / iPad / iTouch
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDrORw0RI-1v-5BixC4T_zLEhcBBwGzyuQ&sensor=true"></script>
// needed for iMac / MacBook
<script src="http://code.google.com/apis/gears/gears_init.js" type="text/javascript"></script>
<script src="http://gaychat.me/js/geo.js" type="text/javascript" ></script>
<script type="text/javascript"><!--
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
var isIPhone = ua.indexOf("iphone") > -1;
var isIPod = ua.indexOf("ipod") > -1;
var isBlackberry = ua.indexOf("blackberry") > -1;
var isIPad = ua.indexOf("ipad") > -1;
if (isIPhone || isIPod || isIPad) {
function doGeo() {
var geolocator = new google.maps.Geocoder();
geolocator.geocode({},
function(results, status){
var geocoding_url = "geocoding.php?lat=" + results[0].geometry.location.lat() + "&long=" + results[0].geometry.location.lng() + "&email=<?=$brosforbros_email;?>&key=<?=$session_key;?>";
$.ajax({
url: geocoding_url,
success: function(data) {}
});
}
)
/*function geo_success(position) {
prev_lat = position.coords.latitude;
prev_long = position.coords.longitude;
var geocoding_url = "geocoding.php?lat=" + position.coords.latitude + "&long=" + position.coords.longitude + "&email=<?=$brosforbros_email;?>&key=<?=$session_key;?>";
$.ajax({
url: geocoding_url,
success: function(data) {}
});
}
function geo_error() {
}
if (!!navigator.geolocation) {
wpid = navigator.geolocation.watchPosition(geo_success, geo_error, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});
}*/
}
} else if (geo_position_js.init()) { // needed for MacBook's / iMac with Safari
function doGeo() {
geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});
function success_callback(p) {
geo_lat = p.coords.latitude.toFixed(3);
geo_long = p.coords.longitude.toFixed(3);
var url = 'geolocation.php?geo_user_id=<?=$user_id;?>&geo_email=<?=$user_email;?>&geo_lat=' + geo_lat + '&geo_long=' + geo_long;
$.ajax({
url: url,
context: document.body,
success: function(data) {
// UPDATE GEOLOCATION
}
});
}
function error_callback(p) {
alert('error='+p.code);
}
}
} else {
var gl;
function getCookie(name) {
var start = document.cookie.indexOf(name + "=");
var len = start + name.length + 1;
if ((!start) && (name != document.cookie.substring(0, name.length))) {
return null;
}
if (start == -1) return null;
var end = document.cookie.indexOf(";", len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}
function displayPosition(position) {
geo_lat = position.coords.latitude;
geo_long = position.coords.longitude;
var url = 'geolocation.php?geo_user_id=<?=$user_id;?>&geo_email=<?=$user_email;?>&geo_lat=' + geo_lat + '&geo_long=' + geo_long;
$.ajax({
url: url,
context: document.body,
success: function(data) {
// UPDATE GEOLOCATION
}
});
}
function displayError(positionError) {
//alert("error");
}
function doGeo() {
try {
if (typeof navigator.geolocation === 'undefined'){
gl = google.gears.factory.create('beta.geolocation');
} else {
gl = navigator.geolocation;
}
} catch(e) {}
if (gl) {
gl.getCurrentPosition(displayPosition, displayError);
} else {
//alert("Geolocation services are not supported by your web browser.");
}
}
}
doGeo();
</script>