2017-09-19 3 views
0

ここでは、ユーザーのジオロケーションをフェッチしてマップに表示して確認するコードロードジオロケーションプラグインを使用しています。私は、空のページが表示されているマップビューを開いたときに、ここで codeova geolocationプラグインを使用してionic app v1でユーザーのジオロケーションを表示および取得する方法

はここ

<ion-view> 
<ion-content> 
    <div id="map" data-tap-disabled="true"></div> 
</ion-content> 
</ion-view> 

map.htmlでindex.htmlを

<ion-nav-view> 

</ion-nav-view> 
    <script src="http://maps.google.com/maps/api/js?key=AIzaSyBvKglWdbBXQc6fL7y1GO8gIVc0"></script> 

では私のコード

のは、コントローラのコード

です
.controller('MapCtrl', function($scope, $state, $cordovaGeolocation) { 
    var options = {timeout: 10000, enableHighAccuracy: true}; 

    $cordovaGeolocation.getCurrentPosition(options).then(function (position) { 

     var latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 

     console.log(position.coords.latitude); 
     console.log(position.coords.longitude); 
     var mapOptions = { 
      center: latLng, 
      zoom: 15, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     $scope.map = new google.maps.Map(document.getElementById("map"), mapOptions); 
     console.log($scope.map); 

     google.maps.event.addListenerOnce($scope.map, 'idle', function(){ 

      var marker = new google.maps.Marker({ 
       map: $scope.map, 
       animation: google.maps.Animation.DROP, 
       position: latLng, 
       icon:'http://i.imgur.com/fDUI8bZ.png' 
      }); 

      var infoWindow = new google.maps.InfoWindow({ 
       content: "Here You Are.!" 
      }); 

      google.maps.event.addListener(marker, 'click', function() { 
       infoWindow.open($scope.map, marker); 
      }); 
     }); 
    }, function(error){ 
     console.log("Could not get location"); 
    }); 
}) 

コンソールで確認すると console.log(position.coords.latitude); console.log(position.coords.longitude); iamがデータを取得していますが、latLngオブジェクトのコンソールでチェックされたときにすべてがnullです。

console.log(latLng); 

答えて

2

あなたがposition.coords.latを取得していれば、あなたのコードロゴジオロケーションが機能している必要があります。

あなたは単に行うことができ、あなたの緯度経度あなたが行った方法をオブジェクトを作成する必要はありません。

var latLng = {lat: position.coords.latitude, lng: position.coords.longitude}

関連する問題