使用 Phonegap 和 Android 的地理位置
Posted
技术标签:
【中文标题】使用 Phonegap 和 Android 的地理位置【英文标题】:GeoLocation using Phonegap and Android 【发布时间】:2013-01-03 10:20:55 【问题描述】:我正在 phonegap 上设计一个移动应用程序。在这我试图获得用户的当前位置。但我没有在 Andriod Emulator 上获得地图。 我使用了这段代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width; height=device-height; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap Map</title>
<link rel="stylesheet" href="/master.css" type="text/css" media="screen" />
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
<script type="text/javascript">
function onLoad()
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
navigator.geolocation.getCurrentPosition(onSuccess, onError,alert("error"),"enableHighAccuracy":true,"timeout":3000);
//GEOLOCATION
var onSuccess = function(position)
alert("Latitude: " + position.coords.latitude + "\n" +
"Longitude:" + position.coords.longitude + "\n");
var myLat = position.coords.latitude;
var myLong = position.coords.longitude;
//MAP
var mapOptions =
center: new google.maps.LatLng(myLat, myLong),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
;
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
;
// onError Callback receives a PositionError object
//
function onError(error)
alert("code: " + error.code + "\n" +
"message: " + error.message + "\n");
</script>
</head>
<body onload="onLoad()">
<div>map</div>
<div id="map_canvas" style="width:100px; height: 100px;" ></div>
</body>
</html>
我不知道我错在哪里。有人可以帮我解决这个问题吗?
【问题讨论】:
你的代码执行了吗?您是否收到任何警报,无论是成功还是错误? 没有大卫,我得到的只是空白页 尝试将navigator.geolocation.getCurrentPosition(onSuccess, onError,alert("error"),"enableHighAccuracy":true,"timeout":3000);
放入onLoad
函数中
Davids- 我试过了,但结果一样。我有一个疑问,当我在 PC 浏览器中运行时,我得到了 Geolocation。AVD 有什么问题>
您是否将 maps.googleapis.com 域列入白名单?
【参考方案1】:
检查此代码...将cordova-1.8.1.js 更改为您的cordova2.0.js...其余源代码请在线参考。如果它不起作用。那么你的phonegap配置有问题。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Info Window Simple</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript" charset ="utf-8" src="cordova-1.8.1.js"></script>
<script>
function initialize()
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions =
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
'<div id="bodyContent">'+
'<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
'sandstone rock formation in the southern part of the '+
'Northern Territory, central Australia. It lies 335 km (208 mi) '+
'south west of the nearest large town, Alice Springs; 450 km '+
'(280 mi) by road. Kata Tjuta and Uluru are the two major '+
'features of the Uluru - Kata Tjuta National Park. Uluru is '+
'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
'Aboriginal people of the area. It has many springs, waterholes, '+
'rock caves and ancient paintings. Uluru is listed as a World '+
'Heritage Site.</p>'+
'<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
'(last visited June 22, 2009).</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow(
content: contentString
);
var marker = new google.maps.Marker(
position: myLatlng,
map: map,
title: 'Uluru (Ayers Rock)'
);
google.maps.event.addListener(marker, 'click', function()
infowindow.open(map,marker);
);
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
</html>
【讨论】:
我没有在你的代码中工作。因为不清楚您是否正确设置了phonegap的环境。所以试试这个。如果它不工作。然后你再次完成整个设置。 RED.Skull- 谢谢,但这是为了通过静态提供 latlng 来获取地图。我得到了它。但我正在尝试当前用户位置,但我没有在 AVD 上得到它。 你能提供你的 logcat 错误信息吗?另一件事是在设备上试试以上是关于使用 Phonegap 和 Android 的地理位置的主要内容,如果未能解决你的问题,请参考以下文章
Phonegap 2.5.0 地理定位不适用于 android 4.0.4