我有以下代码来绘制 map 并将相机动画设置到我当前的位置,只有当我输入精确的纬度和经度(精确的数字)时它才能正常工作,但我需要传递用户当前位置,无论他在哪里,所以我需要知道如何将用户当前位置传递给此
var map;
document.addEventListener("deviceready", function() {
var div = document.getElementById("map_canvas");
// Initialize the map view
map = plugin.google.maps.Map.getMap(div);
// Wait until the map is ready status.
map.addEventListener(plugin.google.maps.event.MAP_READY, onMapReady);
}, false);
function onMapReady() {
// Move to the position with animation
map.animateCamera({
target: {lat: 30.1234567, lng: 31.1234567},
zoom: 17,
tilt: 60,
bearing: 140,
duration: 5000
}, function() {
// Add a maker
map.addMarker({
position: {lat: 30.1234567, lng: 31.1234567},
title: "Welecome to \n" +
"Cordova GoogleMaps plugin for iOS and Android",
snippet: "This plugin is awesome!",
animation: plugin.google.maps.Animation.BOUNCE
}, function(marker) {
// Show the info window
marker.showInfoWindow();
// Catch the click event
marker.on(plugin.google.maps.event.INFO_CLICK, function() {
// To do something...
alert("Hello world!");
});
});
});
}
最佳答案
检查这个tutorial 。使用cordova-geolocation-plugin或Cordova Background Geolocation ,它的工作更加稳定,并且具有更多的功能。您可以将结果传递回 map 插件。
var onSuccess = function(location) {
var msg = ["Current your location:\n",
"latitude:" + location.latLng.lat,
"longitude:" + location.latLng.lng,
"speed:" + location.speed,
"time:" + location.time,
"bearing:" + location.bearing].join("\n");
map.addMarker({
'position': location.latLng,
'title': msg
}, function(marker) {
marker.showInfoWindow();
});
};
var onError = function(msg) {
alert("error: " + msg);
};
map.getMyLocation(onSuccess, onError);
您也可以按照此 SO post 中给出的代码进行操作。
关于javascript - Cordova 谷歌地图插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41220964/