javascript - ionic google map javascript android 设备无法获取当前位置

标签 javascript dictionary ionic-framework

var exampleApp= angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})
exampleApp.controller('MapController', function($scope, $ionicLoading) {
 
   
        var myLatlng = new google.maps.LatLng(37.3000, -120.4833);
 
        var mapOptions = {
            center: myLatlng,
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("map"), mapOptions);
 var marker = new google.maps.Marker({
position: myLatlng,
map: map,
draggable: true,
animation: google.maps.Animation.DROP

 });
        navigator.geolocation.getCurrentPosition(function(pos) {
            map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
            var myLocation = new google.maps.Marker({
                position: new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude),
                map: map,
                title: "My Location"
            });
        });
 
        $scope.map = map;
    });
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-content ng-controller="MapController">
      <div id="map" data-tap-disabled="true"></div>
      </ion-content>
    </ion-pane>
            <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyB1oJGDZP2lM3c1dnPBmAbPBOb_Rh4u8n0&sensor=true"></script>

  </body>
</html>
这部分是代码的javascript和html部分

I dont undestand why the google map unable to get user current location in android device but it work on ios simulator i will be very grateful if anyone of you can help me to solve

最佳答案

直接在 ionic 中使用 navigator 是一个坏主意,我建议使用 ngCordova 及其强大的跨平台功能插件:

http://ngcordova.com/docs/plugins/geolocation/

除此之外,我还会检查您是否为您的应用启用了地理定位。

您应该看到类似这样的内容:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

在您的 AndroidManifest.xml 文件中

关于javascript - ionic google map javascript android 设备无法获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33921833/

相关文章:

javascript - 在 Angular 中将数组数据从按钮推送到表格

javascript - 如何让用户输入文本来搜索数组对象,如果匹配则输出?请纯Javascript

java - 什么将在 Java 中存储键值列表或在 Java 中存储 C# IDictionary 的替代品?

python - 检查值是否存在于数据框中并在字典中找到它

c++ - 在 C++ 中删除映射迭代器的行为不一致

javascript - 如何验证从浏览器中运行的 JS 生成的 html?

javascript - 获得对 CSSMediaRule 的可读访问权限

javascript - 将图像或 div 设置为背景

javascript - 带逗号的 iOS 小键盘输入类型

angular - 当我处于生产模式时,图像不会加载到 ionic 电容器中,但它处于开发模式