Exploring the World with AngularJS and Google Maps API through ngGeolocation

Having some difficulty displaying my geolocation on Google Maps API using a marker.

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

Previously, I hardcoded the marker and map location without any issues. However, upon changing the controller, the map disappeared.

Any assistance would be 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 -->

Thank you!

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

Answer №1

I found a solution that works great 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();
  });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Cutting out the lowest identification that is not a certain object

A real-time counter showing the viewers of a news article on a website using Laravel Jetstream. .leaving(user => { this.participants.splice( this.participants.indexOf(user, 1)); }) .joining(user => { this.participants.push(user); ...

Vue: Duplicating a div element with unique input fields upon clicking

I have successfully set up a page where I can dynamically add a div/row of inputs by clicking the link. This feature works smoothly and adds the same div below the current one with all the proper inputs. Additionally, one of the inputs is designed as an a ...

The parameter value experiences an abrupt and immediate transformation

I recently created an electron app using Node.js and encountered a peculiar issue that I am unable to resolve: Below is the object that I passed as input: { lessons: [ name: "math", scores: [90, 96, 76], isEmpty: false ] } ...

What is the best way to set up TypeScript to utilize multiple node_modules directories in conjunction with the Webpack DLL plugin?

Utilizing Webpack's DllPlugin and DllReferencePlugin, I create a distinct "vendor" bundle that houses all of my main dependencies which remain relatively static. The project directory is structured as follows: project App (code and components) ...

Error encountered when trying to load Ajax script

As a beginner in the learning process, my code may appear messy. I am currently wrapping up my second project, which is a photo viewer. On the main page, there is a navigation system that loads different sections of the website using ajax. Since this proje ...

What could be causing React to throw an error?

Check out this React Component: GeneralInformation = React.createClass({ totalCaptialRaisedPanel: function() { var offeringInfo = this.props.company.data.offerings.data[0]; var percentageComplete = (offeringInfo.capital_raised / offer ...

Avoiding the need to wait for completion of the jQuery $.get function

Currently, I am executing a basic jQuery GET request as shown below and it is functioning correctly. $.get("http://test.example.com/do-something", function (data) { console.log("test work done"); }); The GET request is initiated without affecting the ...

Is there a way for me to ensure that a response is only returned once a method call has been completed in

Seeking assistance with a Node.js application using Express. I am trying to create a REST endpoint that returns the response of an HTTP call. However, no matter what I attempt, it always returns before the HTTP request has completed. Can anyone provide g ...

"NODEJS: Exploring the Concept of Key-Value Pairs in Object

I am facing a challenge with accessing nested key/value pairs in an object received through a webhook. The object in req.body looks like this: {"appId":"7HPEPVBTZGDCP","merchants":{"6RDH804A896K1":[{"objectId&qu ...

What is the best way to invoke a Service from a Directive in AngularJS?

Every time a user clicks, a directive is triggered to change the icon for adding to their favorite list. View: <div class="col col-33"> <i class="icon ion-ios-heart-outline" favorite="place[0].objectId" on-icon="ion-ios-heart-outline" off-ic ...

Incorporating server-side rendered components into a Vue.js parent component

I have a server-side rendered page that includes information about "orders" and a root Vue object initialized as the "parent object." Is it feasible to assign the rendered HTML orders as children of that parent Vue object? Vue.js is known for its dynamic ...

Is it possible to retrieve the complete HTTP response text using Node.js from a HTTP module's .get response?

I have a straightforward web server set up: const ws = require('http'); ws.createServer( function(req,res) { console.log('request received'); res.write('Hello world'); res.end(); }) ...

Is Dart the new AngularJS in terms of programming style?

Is there a software library or project available that allows for programming in Dart using an AngularJS style approach? I am interested in creating annotated HTML files to declare my UI rather than instantiating everything imperatively like with SWT. I wo ...

Having trouble accessing the data I'm setting in a separate component using the Context API

Currently working on my own project and I've encountered a problem while using the Context API. It's my first time using it. The issue I'm facing is that I can't seem to console.log the data I'm setting. I'm trying to create ...

Guide on exporting type definitions and utilizing them with npm link for a local package

I am in the process of developing a new testing tool called tepper as an alternative to supertest. My goal is to make this package available in both ESM and CJS formats. However, I'm encountering an issue where users of the library are unable to locat ...

I am unable to retrieve images using the querySelector method

Trying to target all images using JavaScript, here is the code: HTML : <div class="container"> <img src="Coca.jpg" class="imgg"> <img src="Water.jpg" class="imgg"> <img src="Tree.jpg" class="imgg"> <img src="Alien.jpg" class=" ...

New update in Next.js version 13.4 brings a modification to routing system

I'm currently working with Next.js 13.4 and an app directory. I'm trying to implement a centrally located loader that will show whenever there is a route change anywhere within the app. Since routes/navigation don't have event listeners, I&a ...

Invoke a JavaScript function from a dynamically inserted nested function

My primary application loads JavaScript files based on the page being visited: var MainApp = (function() { function loadPage(folder, template) { $.getScript('scripts/' + template + '.js') .done(function() { ...

Adding TH into a TABLE in Angular 2 by verifying TD elements

I am seeking a way to automatically generate thead and th, using td in the template : <Datatable> <tr #lineSelected *ngFor="let subscription of results"> <td nameColumn="Nom">{{subscription.name}}</td> <td n ...

Developing a specialized directive to enhance bootstrap menuItems

I have created a custom directive in AngularJS for the navbar in Bootstrap. This directive uses ng-transclude and handles two types of list items (li) depending on whether it is a dropdown or not. However, I am experiencing issues with the dropdown functio ...