【问题标题】:how to load Maps after onDeviceReady function in iOS Cordova based app?如何在基于 iOS Cordova 的应用程序中的 onDeviceReady 函数后加载地图?
【发布时间】:2015-10-08 10:00:16
【问题描述】:

我想在 oDeviceReady 函数之后加载地图以避免在 iOS 中访问地图的权限警报,该警报显示完整的捆绑路径位置,如下所示:(iOS Cordova 3.8.0 版本)

为此我使用了

   <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" />
   <script src="cordova.js"></script>
  <link href="css/styles.css" rel="stylesheet" type="text/css">
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
  <script src="js/jquery-1.11.1.min.js"></script>
  <script src="js/jquery.mobile-1.4.5.min.js"></script>
  <script src="js/hashmap.js"></script>

head 标签后:

 </head>
 <body onload="onBodyLoad();">//Called onload

之后

  function onBodyLoad()
  {
         document.addEventListener("deviceready", onDeviceReady, false);
  }


   function onDeviceReady(){      
      return navigator.geolocation.getCurrentPosition(initialize);       
     document.addEventListener("backbutton", onBackKeyDown, false);     
    }

  function initialize() 
 {
   geocoder = new google.maps.Geocoder();
   if (navigator.geolocation) 
   {
      navigator.geolocation.getCurrentPosition(function (p) 
      {
      LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
      map = new google.maps.Map(document.getElementById("estimate"), mapOptions);
     infowindow = new google.maps.InfoWindow();
     marker =new google.maps.Marker({position:LatLng,map:map});
     getAddress(p.coords.latitude,p.coords.longitude);

    }
  }
}

    //This line is making to display complete Bundle path loaction
    //Where as calling this line to avoid error in onDeviceready, My maps are not loading
    // Suggest me where should I call this line of code to avoid this alert
    google.maps.event.addDomListener(window, 'load', initialize);


    function getAddress(lat, lng)
    {
        //Code to get address
     }

谁能建议我应该在哪里打电话来避免这种类型的警报;

  google.maps.event.addDomListener(window, 'load', initialize);

除此警报外,我的代码运行良好...

【问题讨论】:

    标签: ios cordova geolocation phonegap-build onload


    【解决方案1】:

    确保您安装了地理定位插件

    cordova plugin add cordova-plugin-geolocation
    

    并将您的代码更改为

    function onBodyLoad() {
    
        document.addEventListener("deviceready", onDeviceReady, false);
    
    }
    
    
    function onDeviceReady(){      
        navigator.geolocation.getCurrentPosition(initialize);       
        document.addEventListener("backbutton", onBackKeyDown, false);     
    }
    
    function initialize(p) {
        geocoder = new google.maps.Geocoder();
    
        LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
        map = new google.maps.Map(document.getElementById("estimate"), mapOptions);
        infowindow = new google.maps.InfoWindow();
        marker =new google.maps.Marker({position:LatLng,map:map});
        getAddress(p.coords.latitude,p.coords.longitude);
    }
    

    【讨论】:

    • 您是否从 cli 安装了插件?并删除其中一个cordova.js,你有 2
    • 是的,我是从 CMI 安装的,我有一个 cordova.js
    • 实际上,如果我使用“google.maps.event.addDomListener(window, 'load', initialize);”除了它在代码中的当前位置(如 onDeviceReady 地图)之外的任何其他地方都没有加载..您能否建议我使用其他方法来删除这种错误警报。它正在显示 js 文件的完整位置
    • 你能用你当前的代码更新你的问题吗?在您发布的代码中,您有两个cordova.js,并且您多次调用initializa,您必须按照我告诉您的方式调用它,删除窗口加载行和任何其他调用初始化的行,只留下一个来自onDeviceReady
    • 非常感谢..删除窗口加载线后,它现在正在工作(获取默认的 iOS 地图权限警报)..
    【解决方案2】:

    首先使用命令提示符(cmd)添加这个插件

    cordova 插件添加 org.apache.cordova.geolocation

    <div id="map" style="width:100%; height:500px;"></div>
    
    <script> 
    $(document).ready(function () { 
        var geocoder;
        var map;
        function initialize() 
        {
            geocoder = new google.maps.Geocoder();
            var mapOptions = {
                zoom: 12
            };
            map = new google.maps.Map(document.getElementById('map'), mapOptions);
            codeAddress();
        }
    
        google.maps.event.addDomListener(window, 'load', initialize);
    
        if (navigator.geolocation) 
        {
            navigator.geolocation.getCurrentPosition(function (position) {
            initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            map.setCenter(initialLocation);
            });
        }
    
    });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 2014-06-16
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多