javascript - Cordova 谷歌地图插件

标签 javascript google-maps phonegap-plugins cordova-plugins

我有以下代码来绘制 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-pluginCordova 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/

相关文章:

android - 如何将 map.jar 添加到 android 中的 myproject 中?

html - 在新的 Google Maps Engine 上禁用 Zoom

cordova - 为什么使用 Ionic cordovaCapture(Apache Cordova 媒体捕获插件)在 iOS 上录制低分辨率视频 (480x360)?

javascript - 如何在WebGl运行时绘制各种大小的各种形状?

javascript - Google map 圆半径不起作用的问题

javascript - 仅在 CSS 中为嵌套元素选择立即悬停的元素

android - Phonegap 无法识别 www 文件夹资源

javascript - 连接和缩小 cordova/phonegap 插件文件

javascript - 在 IE 中阻止脚本

javascript - scrollOverflow/slimScroll 在 fullPage.js 中无法正常工作