【发布时间】:2017-02-06 06:34:41
【问题描述】:
这是我用来构建 Google 地图的代码,它适用于 80% 的 Android 手机,但本地手机制造商不支持此功能,因为我无法在 OPPO、HONOR 等手机中获取坐标。
我尝试了 Google Geolocation API,它没有提供准确的 latlng。
function inti () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoLocationSucess);
backview = "views/home.html";
}
}
function writeAddress(latLng) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"location": latLng
},
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var rr = results[0].formatted_address;
var res = rr.split(",");
// alert(res[0] + ", " +res[1]+ "," + res[2]);
} else
"Unable to retrieve your address" + "<br />";
});
}
function initialize() {
//checker to watch position every sec if position fixed it will not refresh
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoLocationSucess, showError);
} else
alert("Your Device does't support geolocation.");
}
function geoLocationSucess(position) {
var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
writeAddress(userLatLng)
var myOptions = {
zoom: 15,
center: userLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var mapObject = new google.maps.Map(document.getElementById("googleMap"), myOptions);
var marker = new google.maps.Marker({
map: mapObject,
position: userLatLng
});
var sunCircle = {
strokeColor: "#7DB1E8",
strokeOpacity: 0.5,
strokeWeight: 2,
fillColor: "#7DB1E8",
fillOpacity: 0.25,
map: mapObject,
center: userLatLng,
radius: 200 // in meters
};
var cityCircle = new google.maps.Circle(sunCircle);
cityCircle.bindTo(center, marker);
}
function showError(error) {
switch (error.code) {
case 1:
alert("User denied the request for Geolocation.");
break;
case 2:
alert("Location information is unavailable. Please ensure Location is On");
break;
case 3:
break;
case 4:
alert("An unknown error occurred.");
break;
}
}
<!DOCTYPE html>
<html>
<style>
#googleMap {
width: 100%;
height: 80vh;
}
</style>
<body>
<div id="googleMap"> </div> <span id="locate" data-role="touch"><a data-role="button" onclick="initialize()"
id="locate" style=" width:50%;background:#18515C;" >Locate Me
</a></span> <span id="punch" data-role="touch"><a data-role="button" onclick="punch()"
id="punch" style="width:40%;background:#18515C; ">Punch
</a></span>
<script src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
</body>
</html>
【问题讨论】:
-
你是在使用地理定位插件cordova还是html5地理定位api??
-
我正在使用 html5 地理定位 api
标签: javascript android html cordova geolocation