Google Map Javascript Api, Angular, Dynamicly Loaded Page - angularjs

What happened past weeks i don't know if google messed things up again. My map stopped working.
I transferred the app Angular for routes. When the contact page loads, initializeMap function called.
I got new browser api key from google console which is not like old. I enabled Javascript Map Api. I entered my URLs in credential.
Sometimes it gives error invalidkey*** but only sometimes.
It works on my domain if i put everything in one single file.
It also works on codepen. I don't know why? There is no error to debug this thing. My beloved javascript.
http://codepen.io/emrahatilkan/pen/bVwzrR
Edit: This is about angular definitely.
Google Map Link in index.html:
<script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY"></script>
My URL: index.html/#/contact
I don't know if it is relevant but contact.html is inside pages/ folder.
My Code is in main.js:
function initializeMap() {
console.log('init map'); //this outputs to console
var mapOptions = {
center: { lat: 39.7944417, lng: 30.4935026 },
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scaleControl: false,
scrollwheel: false
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
title: 'Some Title'
});
var infowindow = new google.maps.InfoWindow({
content: '<h5>Some Title</h5>'
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
}

Related

Google Map not loading AngularJs

I am trying to show a google map on my AngularJs app. I used below code and when run, map not displayed. Just a blank page. No errors on browser console.
Here is the Fiddle
HTML Code
<div class="row">
<div id="google-maps"></div>
</div>
Controller Code
function initialize() {
var mapCanvas = document.getElementById('google-maps');
var mapOptions = {
center: new google.maps.LatLng(44.5403, -78.5463),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(mapCanvas, mapOptions);
}
$timeout(function(){
initialize();
}, 100);
Also I have this code in index.html file
<script src="https://maps.googleapis.com/maps/api/js?key=my_key&libraries=places"></script>
Try calling initialize() inside document.ready:
$(initialize);
Also give a width and height to your container div.
Working Fiddle

How to make Google Maps Load only Once between tabs in Ionic 1 Tabs

I have two tabs: Maps and Places
The problem is that every time I go the Maps tab, the Google Maps it is loaded whether is already loaded before. This causes my app to slow down, every time I go to the Maps Tab
I tried to make this logic my MapCtrl
if ($scope.map == undefined) {
console.log('NO GOOGLE MAP');
var mapOptions = {
center: usersLatLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
$cordovaGeolocation.getCurrentPosition(options).then(function(position){
$scope.map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
} else {
console.log('HAS GOOGLE MAP');
}
The result in the console every time I go to the Maps Tab is 'NO GOOGLE MAP'
Is there anyway to trigger the else function when the Google Maps is already defined?
Example would be when I move from MAPS->PLACES->MAPS, the Google Map won't load anymore.
I fixed the problem by adding cache:true in my state.
.state('tab.map', {
url: '/map',
cache:true,
views: {
'tab-map': {
templateUrl: 'templates/tab-map.html',
controller: 'MapCtrl'
}
},
params: { args: {} }
})

How to add info window for custom overlays?

I have implemented a custom overlay. I want to add an event which will pop an info window when I click the overlay.
function initMap() {
var map = new google.maps.Map(document.getElementById('okmap'), {
zoom: 4,
center: {lat: 35.0000, lng: 103.0000}
});
for (var n in nodes) {
var myMarker = new MyMarker(map, {latlng: new google.maps.LatLng(nodes[n][0], nodes[n][1]),
image: 'assets/img/light-green.png',
labelText: (Math.random() * 100).toFixed(0)});
markers.push(myMarker);
var infowindow = new google.maps.InfoWindow({
content: n
});
myMarker.addListener('click', function() {
infowindow.open(map, myMarker);
});
}
}
But it is not working!
I faced a simliar problem some time back. Here are two things that helped me.
I am considering the main overlay element to be a div.
In your MyMarker.draw function make sure that you are using a pane that has access to the DOM events.e.g.
var panes = this.getPanes();
panes.overlayMouseTarget.appendChild(div);
See https://developers.google.com/maps/documentation/javascript/customoverlays#initialize for more details.
Make sure you define an event listener, also in the MyMarker.draw function.
google.maps.event.addDomListener(div, "click", function(event) {
google.maps.event.trigger(self, "click");
});

Access InfoWindow from angular google map

I am using google maps via angularJS (using angular-google-map)
I have a marker with an InfoWindow which works fine except on iOS. When I rotate the device, the google map API doesn't seem to calculate the size well and the infoWindow is too small for its content. I reckon this is the same bug as this one
So I have been trying to access the InfoWindow object to close it and open it again to force google to redraw/recalculate but couldn't find a way to access it.
I can access the google.maps.Map object by using $scope.map.control.getGMap() in angular but can't access the InfoWindow. I can see a getGWindows in Window.prototype but no idea how to access it from the map. I can access the markers as well but they don't have a reference to their infowindow.
Here is the way I create the marker in case it can be of any help:
var marker = {
id: obj.id,
name: obj.name,
latitude: obj.lat,
longitude: obj.lng,
address: obj.address,
suburbCity: obj.suburbCity,
state: obj.state,
postcodeZip: obj.postcodeZip,
icon: "xxx.png"),
options: {
labelContent: obj.name,
labelClass: "none",
labelAnchor:"25 0",
opacity: 0.25,
animation: google.maps.Animation.DROP,
visible: true
},
templateurl:'markerwindow.tpl.html',
templateparameter: data,
closeClick: function() {
marker.options.visible = false;
return $scope.$apply();
},
onClicked: function() {
navigateToMarkerOnMap(marker);
}
};
$scope.map.markers.push(marker);
function navigateToMarkerOnMap(marker) {
$scope.selected.options.visible = false;
$scope.selected = marker;
$scope.selected.options.visible = true;
}

Integrating angularjs ui.map with geo location api

I am using
http://jsfiddle.net/xSPAA/371/
//Add the requried module 'angular-ui' as a dependency
angular.module('maptesting', ['ui.map','ui.event']);
function MapCtrl($scope) {
var ll = new google.maps.LatLng(45, -73);
$scope.mapOptions = {
center: ll,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//Markers should be added after map is loaded
$scope.onMapIdle = function() {
if ($scope.myMarkers === undefined){
var marker = new google.maps.Marker({
map: $scope.myMap,
position: ll
});
$scope.myMarkers = [marker, ];
}
};
$scope.markerClicked = function(m) {
window.alert("clicked");
};
}
plunker to show google map. I need to show a specific location map that a user will enter into a text box.
How can I do it?
Do I need to use Geo Location API?
Can any one give sample code or plunker?
Below is the working example :
http://www.victorshi.com/blog/post/Use-Geolocation-API-with-Angularjs
Example
Also check below url:
http://www.benfarrell.com/2013/11/05/some-geolocation-and-google-mapping-services-in-angularjs/
Google Location Service
http://jsfiddle.net/mrajcok/pEq6X/
directive 'googlePlaces'

Resources