gpt4 book ai didi

android - 如何使用 Cordova Geolocation 插件在 Ionic App 中添加标记

转载 作者:行者123 更新时间:2023-11-29 20:58:02 26 4
gpt4 key购买 nike

我对这段代码有疑问。我可以正确看到 Google map ,我的实际位置位于 android 屏幕的中央,但无论我做什么我都看不到标记。

这是 html:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<style>
html, body {
height:100%;
}
</style>
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
</head>
<body ng-app="starter" ng-controller="GeoCtrl" class="platform-android">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>

这是 services.js

angular.module('starter.services', [])

.factory('Location', ['$q', function($q) {
return {
getLocation: function(options) {
var q = $q.defer();
navigator.geolocation.getCurrentPosition(function(position) {
q.resolve(position);
}, function(err) {
q.reject(err);
});
return q.promise;
}
}
}]);

这是 app.js:

angular.module('starter', ['ionic', 'starter.services'])
.run(function($ionicPlatform) {
...
})
.controller('GeoCtrl', function($scope, Location) {
$scope.lat;
$scope.long;
$scope.map;
$scope.marker;
Location.getLocation().then(function (position) {
$scope.lat = position.coords.latitude;
$scope.long = position.coords.longitude;
var mapOptions = {
center: new google.maps.LatLng($scope.lat, $scope.long),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
$scope.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}, function(err) {
// error
});
$scope.marker = new google.maps.Marker({
position: new google.maps.LatLng($scope.lat, $scope.long),
map: $scope.map,
title: 'Holas!'
}, function(err) {
console.err(err);
});
});

谢谢!

最佳答案

我希望这可以帮助某人:

我在 then 流外部调用了标记,而它必须在内部调用:

  Location.getLocation().then(function (position) {
$scope.lat = position.coords.latitude;
$scope.long = position.coords.longitude;
var mapOptions = {
center: new google.maps.LatLng($scope.lat, $scope.long),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
$scope.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
$scope.marker = new google.maps.Marker({
position: new google.maps.LatLng($scope.lat, $scope.long),
map: $scope.map,
title: 'Holas!'
}, function(err) {
console.err(err);
});
}, function(err) {
// error
});

现在可以了。

关于android - 如何使用 Cordova Geolocation 插件在 Ionic App 中添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27037549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com