简体   繁体   English

用Ionic包装Angular2应用-地理位置问题

[英]Wrapping an Angular2 App with Ionic - Geolocation Issues

I have an existing, responsive, Angular 2 application that I would like to wrap using Ionic and deploy to the app store. 我有一个现有的响应式Angular 2应用程序,我想使用Ionic包装并部署到应用程序商店。 The application will run on both web browsers, and as a native app on iOS and Android . 该应用程序将在两种Web浏览器上运行,并在iOS和Android上作为本机应用程序运行。

I've got this mostly worked out now, with one problem. 我现在已经解决了大部分问题,但有一个问题。 I need to use geolocation on my first screen and my web app does this with navigator.geolocation.getCurrentPosition as you'd expect. 我需要在第一个屏幕上使用geolocation ,并且我的Web应用程序按照您期望的那样通过navigator.geolocation.getCurrentPosition此操作。 This works great in the web, asking the user for permission and then performing the geolocation . 这在Web上效果很好,需要先征求用户的许可,然后再进行geolocation

In the iOS version, after I tag in the cordova-plugin-geolocation I can get a message to pop-up to allow geolocation as well, but this message uses the long path 在iOS版本中,我标记了cordova-plugin-geolocation我可以弹出一条消息以允许进行地理定位,但是该消息使用的是长路径

("/var/containers/bundle/application/{{GUID}}/{{APPNAME}}/www/index.html" would like to use your current location). (“ /var/containers/bundle/application/{{GUID}}/{{APPNAME}}/www/index.html”想使用您当前的位置)。

This looks hacky and amateurish and honestly, I'd probably delete any app that gave me a message like that. 老实说,这看起来很笨拙,我可能会删除所有给我这样信息的应用程序。 I'm trying to figure out how to get this message to just say the name of my app. 我试图弄清楚如何获得此消息,只是说出我的应用程序的名称。 Here are some things I've tried... 这是我尝试过的一些事情...

  • Adding NSLocationWhenInUseUsageDescription and it's variations to my plist file. 添加NSLocationWhenInUseUsageDescription及其它对我的plist文件的变体。 This results in me getting a message with the app name first, and then getting a second message with the path. 这导致我首先收到一条带有应用程序名称的消息,然后再收到第二条带有路径的消息。
  • Importing 'ionic-native' in my angular 2 app and trying to use Geolocation as a wrapper -- no change 在我的angular 2应用程序中导入“ ionic-native”并尝试使用Geolocation作为包装器-不变
  • Waiting for the "deviceready" event to do geolocation -- no change 等待“ deviceready”事件进行地理位置定位-无需更改
  • Setting a long delay (up to 30 seconds) with setTimeout before doing geolocation -- no change 在进行地理位置定位之前,使用setTimeout设置较长的延迟时间(最多30秒)-无需更改

I hate the idea that I have to have two code bases just to deploy a simple web app via the app store distribution channel. 我讨厌只有两个代码库才能通过应用程序商店分发渠道部署简单的Web应用程序。 Surely there's some way around this that I just haven't thought of yet. 当然,有一些解决方法我还没有想到。 Any suggestions? 有什么建议么?

So, this turned out to be somewhat simple, actually. 因此,实际上这实际上很简单。 The built index.html file just needs to have the ionic javascript references, like this: 生成的index.html文件只需要具有离子javascript引用,如下所示:

<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>

I modified my gulp file that processes by webpack build for ionic to throw these in right before the closing head tag and now my call to geolocation works with proper messaging. 我修改了我的gulp文件,该文件由ionic的webpack构建进行处理,以将它们放在结束head标签之前,并且现在我对geolocation的调用可与适当的消息传递一起使用。

navigator.geolocation.getCurrentPosition((pos) => { 
   //do things with pos
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM