4

I am having trouble getting a marker to display my geolocation on google maps api.

I am using the ng controller ngGeolocation & the http://angular-ui.github.io/angular-google-maps/

I had no troubles when I hardcoded the marker and map location. But as soon as I changed the controller the map disappeared.

Any help greatly appreciated

My controller

'use strict';

angular.module('fiveMinCatchupApp')
.controller('MainCtrl', function ($scope, uiGmapGoogleMapApi) {

var scope = $scope;
var lat; 
var lng

function getLocation() {
navigator.geolocation.getCurrentPosition($scope.showLocation);
};

$scope.showLocation = function(position){
  lat = position.coords.latitude;
  lng = position.coords.longitude;
  $scope.map = {
  center: {
    latitude: position.coords.latitude,
    longitude: position.coords.longitude,
  },
  zoom: 8
}

};

getLocation();

Main html

<ui-gmap-google-map center="map.center" zoom="map.zoom">
<ui-gmap-markers 
models='markers'
coords="'coords'"> 
</ui-gmap-markers>
</ui-gmap-google-map>

HTML

<body ng-app="fiveMinCatchupApp">


<div class="container">
<div ng-view=""></div>
</div>

<div class="footer">
  <div class="container">
    <p><span class="glyphicon glyphicon-heart"></span> from the Yeoman team</p>
  </div>
</div>


<!-- Google Analytics: change UA-XXXXX-X to be your site's ID -->
 <script>
   !function(A,n,g,u,l,a,r){A.GoogleAnalyticsObject=l,A[l]=A[l]||function(){
   (A[l].q=A[l].q||[]).push(arguments)},A[l].l=+new Date,a=n.createElement(g),
   r=n.getElementsByTagName(g)[0],a.src=u,r.parentNode.insertBefore(a,r)
   }(window,document,'script','//www.google-analytics.com/analytics.js','ga');

   ga('create', 'UA-XXXXX-X');
   ga('send', 'pageview');
</script>

<script src='//maps.googleapis.com/maps/api/js?sensor=false'></script>

<!-- build:js(.) scripts/vendor.js -->
<!-- bower:js -->
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script src="bower_components/angular-animate/angular-animate.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-touch/angular-touch.js"></script>
<script src="bower_components/lodash/dist/lodash.compat.js"></script>
<script src="bower_components/angular-google-maps/dist/angular-google-maps.js"></script>
<script src="bower_components/ngGeolocation/ngGeolocation.js"></script>
<!-- endbower -->
<!-- endbuild -->

    <!-- build:js({.tmp,app}) scripts/scripts.js -->
    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/main.js"></script>
    <script src="scripts/controllers/about.js"></script>
    <!-- endbuild -->

Thanks!

Previous version hardcoded on github at https://github.com/5-minute-catchup/5-min-catchup/commit/2899e013a78e93016154f1bd7d462b83f414db7e

2
  • Do you still have the code that assigns $scope.map variable? It should use your myPosition.coords latitude and longitude to calculate map.center. Commented May 19, 2015 at 15:55
  • I see! yes I had missed that bit of code off.. Thanks for the tip, will see if I can work this out. Commented May 19, 2015 at 16:03

1 Answer 1

3

This seems to do the trick for me:

angular.module('fiveMinCatchupApp').controller('MainCtrl', function ($scope, uiGmapGoogleMapApi) {

$scope.map = { center: { latitude: 45, longitude: -73 }, zoom: 8 };

  navigator.geolocation.getCurrentPosition(function(pos) {
    $scope.map.center = {
     latitude: pos.coords.latitude,
     longitude: pos.coords.longitude
    };
    $scope.$apply();
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.