How to modify google maps clustering algorithm / options - google-maps

I have 500+ markers on my javascript google map and working on adding clustering.
Having issues setting some simple options and custom images using
js-markerclusterer Public Github library.
Following this Google developer guide guide and adding the library using CDN
I expected to set my options like this:
const clusterOptions = {
imagePath: "<imagepath>",
gridSize: 30,
zoomOnClick: false,
maxZoom: 10,
};
const markerCluster = new markerClusterer.MarkerClusterer({ map, markers, clusterOptions});
This has no effect on the clusters.
What is the correct way of setting cluster options?
Here is a bigger snippet of my code:
function initMap() {
// Create the map.
var lastWindow = null;
var markers = new Array();
const map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
keyboardShortcuts: false,
center: { lat: 40.748817, lng: -73.985428 },
disableDefaultUI: true,
gestureHandling: "greedy",
ClickableIcons: false,
styles: mapStyle,
restriction: {
latLngBounds: NEW_YORK_BOUNDS,
strictBounds: true
},
});
const markers_map = {{markers_map | tojson | safe}};
const infowindow = new google.maps.InfoWindow();
// Create markers.
for (let i = 0; i < markers_map.length; i++) {
const currMarker = markers_map[i];
const marker = new google.maps.Marker({
position: {lat: currMarker.lat, lng: currMarker.lng},
map,
animation: google.maps.Animation.DROP,
icon: currMarker.icon
});
marker.addListener("click", () => {
if (lastWindow) {lastWindow.close()};
lastWindow = infowindow;
const _id = currMarker._id;
const name = currMarker.name;
const url_link = currMarker.url_link;
const address = currMarker.address;
const neighborhood = currMarker.neighborhood;
const from_day = currMarker.from_day;
const to_day = currMarker.to_day;
const from_hours = currMarker.from_hours;
const to_hours = currMarker.to_hours;
const content = `
<div class=infowindow>
<div class=infowindow__imagecontainer>
<img class=infowindow__image src=${url_link} alt=HappyHour img>
</div>
<div class=infowindow__content onclick=nextpage()>
<h2 class=markers__name>${name}</h2><p class=markers__neighborhood>In ${neighborhood}</p>
<div class=markers__section>
<p class=markers__address>${address}</p>
{{ map("container__map") }}
</div>
<div class=infowindow__output>
<div class=output__details>${from_day}</div>
<div class=output__details>${to_day}</div>
<div class=output__details>${from_hours}</div>
<div class=output__details>${to_hours}</div>
</div>
<div class=infowindow__next>
{{ click("container__click") }}
<p>Click to see more</p>
</div>
<div>
<form name=infowindow--form id=next action=/location/${_id}></form>
</div>
` ;
infowindow.open({
anchor: marker,
map,
shouldFocus: false,
});
infowindow.setContent(content)
});
markers.push(marker);
map.addListener('click', () => {
infowindow.close();
});
}
const clusterOptions = {
imagePath: "<imagepath>",
gridSize: 30,
zoomOnClick: false,
maxZoom: 10,
};
const markerCluster = new markerClusterer.MarkerClusterer({ map, markers, clusterOptions});

Related

Google maps displaying other side of the planet

I have literally been trying anything and everything, but the map displays somewhere in Antarctica, when the coordinates that I am receiving, are for cities in the US.
The following is the code that inits the map:
$(window).on('load', function() {
// eslint-disable-next-line
function initTest() {
var fastestCitiesApi = JSON.parse(document.getElementById('fastestCitiesApi').value);
console.log(fastestCitiesApi);
var tempLatLng = fastestCitiesApi[0]['latLng'];
console.log(tempLatLng);
var latLng = tempLatLng.split(',');
console.log('latLng[0]: ' + latLng[0]);
console.log('latLng[1]: ' + latLng[1]);
const myLatLng = new google.maps.LatLng({ lat: parseFloat(latLng[0]), lng: parseFloat(latLng[1]) });
const mapOptions = {
zoom: 4,
center: myLatLng
};
const map = new google.maps.Map(document.getElementById('fastest_cities_map'), mapOptions);
new google.maps.Marker({
position: myLatLng,
map,
title: 'somewhere',
});
}
initTest();
});
As you can see by the following screen shot of the console is showing, that the coordinates are 36, 78 - which when I do a look up on google for Boydton, VA - those are the coordinates that is returned - link to google search for Boydton, VA lat long
As you can see by the screen shot, I have zero errors. What am I doing wrong?
Thanks in advance
Thanks to #Gray pointing out that the long should be negative, the rest of the array loaded up the markers as expected.
Here's the final code:
$(window).on('load', function() {
// eslint-disable-next-line
function initTest() {
/* init vars */
const map = new google.maps.Map(document.getElementById('fastest_cities_map'));
var fastestCitiesApi = JSON.parse(document.getElementById('fastestCitiesApi').value);
// console.log(fastestCitiesApi);
var tempLatLng = '';
var latLng = [];
var markerOptionsArray = new Array();
var latlngArray = new Array();
var myLat = new Array();
var myLng = new Array();
var markers = [];
var nameArray = [];
var infowindow = new google.maps.InfoWindow({maxWidth: 350});
/* create arrays for markers */
for(var item in fastestCitiesApi) {
tempLatLng = fastestCitiesApi[item]['latLng'];
nameArray.push(fastestCitiesApi[item]['city']);
// console.log(tempLatLng);
latLng = tempLatLng.split(',');
myLat.push(latLng[0]);
myLng.push(latLng[1]);
// console.log('latLng[0]: ' + latLng[0]);
// console.log('latLng[1]: ' + latLng[1]);
markers.push({
lat: myLat[item],
lng: myLng[item],
name: nameArray[item],
});
}
// console.log('markers: ' + JSON.stringify(markers));
var bounds = new google.maps.LatLngBounds();
for (var v=0; v<markers.length; v++)
{
var secheltLoc = new google.maps.LatLng(markers[v].lat, markers[v].lng);
var myOptions = {
content: markers[v].name
,boxStyle: {
border: "none"
,textAlign: "center"
,fontSize: "12pt"
,width: "120px"
}
,disableAutoPan: true
,pixelOffset: new google.maps.Size(-50, 0)
,position: secheltLoc
,closeBoxURL: ""
,isHidden: false
,pane: "mapPane"
,enableEventPropagation: true
};
var marker = new google.maps.Marker({
position: new google.maps.LatLng(markers[v].lat, markers[v].lng),
map: map,
center: new google.maps.LatLng(markers[v].lat, markers[v].lng)
});
google.maps.event.addListener(marker, 'click', (function (marker, v) {
return function () {
infowindow.setContent(markers[v].name);
infowindow.open(map, marker);
}
})(marker, v));
google.maps.event.addListener(map, 'click', function () {
infowindow.close();
marker.open = false;
});
bounds.extend(new google.maps.LatLng(markers[v].lat, markers[v].lng));
myLatLng = markers[v].lat+','+markers[v].lng;
}
map.fitBounds(bounds);
}
// alert(google.maps.Map);
initTest();
});
The following is a screen shot of the result of the eight city array:

Trying to get the marker var out of the foreach loop for markerCluster to work, what is the easiest way of doing this?

I am trying to add MarkerClusters to my map, however my marker variable is within a function with a foreach loop used to retrieve Instagram API data, what is the best possible way to get MarkerClusters working?
I tried wrapping the initMap function around the setMarkers function, putting the markerCluster variable within the setMarkers function and within the foreach loop but it just keeps showing the markers (pictures in my case)
<script>
let coords = document.getElementById("places").innerHTML;
let parts = coords.split(",");
let finalResult = []
while (parts.length) {
let newArr = parts.splice(0, 3);
finalResult.push(newArr);
}
console.log(finalResult)
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {
lat: 52.9,
lng: 101.2
}
});
setMarkers(map);
}
function setMarkers(map) {
finalResult.forEach((place) => {
var image = {
url: place[0],
scaledSize: new google.maps.Size(64, 64),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(32, 32)
};
var shape = {
coords: [1, 1, 1, 20, 18, 20, 18, 1],
type: 'poly'
};
var myLatlng = new google.maps.LatLng(place[1], place[2]);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
icon: image,
shape: shape
});
})
var markerCluster = new MarkerClusterer(map, marker,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
</script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
</script>
One option is to create the MarkerClusterer inside your setMarkers function, then add the markers to it individually as you create them with the .addMarker method.
function setMarkers(map) {
var markerCluster = new MarkerClusterer(map, [], {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
finalResult.forEach((place) => {
var image = {
url: place[0],
};
var myLatlng = new google.maps.LatLng(place[1], place[2]);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
icon: image,
});
markerCluster.addMarker(marker);
});
proof of concept fiddle
code snippet:
let coords = document.getElementById("places").innerHTML;
let parts = coords.split(",");
let finalResult = []
while (parts.length) {
let newArr = parts.splice(0, 3);
finalResult.push(newArr);
}
console.log(finalResult)
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {
lat: -38,
lng: 150
}
});
setMarkers(map);
}
function setMarkers(map) {
var bounds = new google.maps.LatLngBounds();
var markerCluster = new MarkerClusterer(map, [], {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
finalResult.forEach((place) => {
var image = {
url: place[0],
};
var myLatlng = new google.maps.LatLng(place[1], place[2]);
bounds.extend(myLatlng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
icon: image,
});
markerCluster.addMarker(marker);
map.fitBounds(bounds);
});
}
html,
body,
#map {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<div id="places" style="display:none">https://maps.google.com/mapfiles/ms/micons/blue.png,-31.563910,147.154312, https://maps.google.com/mapfiles/ms/micons/blue.png,-33.718234,150.363181, https://maps.google.com/mapfiles/ms/micons/blue.png,-33.727111,150.371124, https://maps.google.com/mapfiles/ms/micons/blue.png,-33.848588,151.209834,https://maps.google.com/mapfiles/ms/micons/blue.png,-33.851702,151.216968,
https://maps.google.com/mapfiles/ms/micons/blue.png,-34.671264,150.863657, https://maps.google.com/mapfiles/ms/micons/blue.png,-35.304724,148.662905, https://maps.google.com/mapfiles/ms/micons/blue.png,-36.817685,175.699196,https://maps.google.com/mapfiles/ms/micons/blue.png,-36.828611,175.790222,
https://maps.google.com/mapfiles/ms/micons/blue.png,-37.750000,145.116667, https://maps.google.com/mapfiles/ms/micons/blue.png,-37.759859,145.128708, https://maps.google.com/mapfiles/ms/micons/blue.png,-37.765015,145.133858,https://maps.google.com/mapfiles/ms/micons/blue.png,-37.770104,145.143299,
https://maps.google.com/mapfiles/ms/micons/blue.png,-37.773700,145.145187, https://maps.google.com/mapfiles/ms/micons/blue.png,-37.774785,145.137978, https://maps.google.com/mapfiles/ms/micons/blue.png,-37.819616,144.968119, https://maps.google.com/mapfiles/ms/micons/blue.png,-38.330766,144.695692,
https://maps.google.com/mapfiles/ms/micons/blue.png,-39.927193,175.053218, https://maps.google.com/mapfiles/ms/micons/blue.png,-41.330162,174.865694, https://maps.google.com/mapfiles/ms/micons/blue.png,-42.734358,147.439506, https://maps.google.com/mapfiles/ms/micons/blue.png,-42.734358,147.501315,
https://maps.google.com/mapfiles/ms/micons/blue.png,-42.735258,147.438000, https://maps.google.com/mapfiles/ms/micons/blue.png,-43.999792,170.463352,
</div>

Google Maps V3 - Load different markers on click function

So I have the following in my HTML which represents regions in the UK:-
<h4 id="google-ne" class="active">The North East</h4>
<h4 id="google-nw">The North West</h4>
<h4 id="google-ea">East Anglia</h4>
<h4 id="google-em">East Midlands</h4>
<h4 id="google-tm">The Midlands</h4>
<h4 id="google-wm">West Midlands</h4>
<h4 id="google-ld">London</h4>
<h4 id="google-se">South East</h4>
<h4 id="google-sw">South West</h4>
<h4 id="google-ws">Wales</h4>
<h4 id="google-sl">Scotland</h4>
and then the marker lat / long and region are displayed in HTML as follows:-
<div class="marker" data-lat="52.559437" data-lng="-2.1493073" data-region="West Midlands"></div>
<div class="marker" data-lat="51.646145" data-lng="-0.45614472" data-region="South East"></div>
and so on, there are about 400 markers.
I am currently using the following code to display all markers on the map which is working fine:-
var center = new google.maps.LatLng(51.5280359,-0.1304897);
function initialize_map() {
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: 'roadmap'
};
var markerBounds = new google.maps.LatLngBounds();
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var isDraggable = w > 480 ? true : false;
var mapOptions = {
zoom: 8,
center: center,
//draggable: isDraggable,
//mapTypeControl: false,
//draggable: false,
zoomControl: true,
mapTypeControl: true,
scaleControl: true,
scrollwheel: true,
navigationControl: true,
streetViewControl: true,
disableDefaultUI: true
};
var map = new google.maps.Map(document.getElementById('map'),
mapOptions);
// Multiple Markers
// Loop through our array of markers & place each one on the map
$('.marker').each(function() {
var location = {
latLng: new google.maps.LatLng(
$( this ).data( 'lat' ),
$( this ).data( 'lng' )
),
//title: $( this ).find( 'h2' ).html()
};
new google.maps.Marker( {
map: map,
position: location.latLng,
//title: $( this ).data( 'desc' )
} );
markerBounds.extend( location.latLng );
});
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
this.setZoom(14);
google.maps.event.removeListener(boundsListener);
});
var styles = [
/* Black & White {"featureType":"water","elementType":"geometry","stylers":[{"color":"#e9e9e9"},{"lightness":17}]},{"featureType":"landscape","elementType":"geometry","stylers":[{"color":"#f5f5f5"},{"lightness":20}]},{"featureType":"road.highway","elementType":"geometry.fill","stylers":[{"color":"#ffffff"},{"lightness":17}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"color":"#ffffff"},{"lightness":29},{"weight":0.2}]},{"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":18}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":16}]},{"featureType":"poi","elementType":"geometry","stylers":[{"color":"#f5f5f5"},{"lightness":21}]},{"featureType":"poi.park","elementType":"geometry","stylers":[{"color":"#dedede"},{"lightness":21}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"on"},{"color":"#ffffff"},{"lightness":16}]},{"elementType":"labels.text.fill","stylers":[{"saturation":36},{"color":"#333333"},{"lightness":40}]},{"elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"transit","elementType":"geometry","stylers":[{"color":"#f2f2f2"},{"lightness":19}]},{"featureType":"administrative","elementType":"geometry.fill","stylers":[{"color":"#fefefe"},{"lightness":20}]},{"featureType":"administrative","elementType":"geometry.stroke","stylers":[{"color":"#fefefe"},{"lightness":17},{"weight":1.2}]} */
/* Colour*/ {"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"saturation":"-63"},{"lightness":"23"}]},{"featureType":"landscape.natural","elementType":"geometry.fill","stylers":[{"saturation":"-100"},{"lightness":"25"}]},{"featureType":"landscape.natural.terrain","elementType":"geometry.fill","stylers":[{"saturation":"0"}]},{"featureType":"poi.park","elementType":"geometry.fill","stylers":[{"saturation":"0"},{"color":"#95bf97"},{"lightness":"59"}]},{"featureType":"poi.school","elementType":"geometry.fill","stylers":[{"lightness":"5"},{"hue":"#ff0000"},{"saturation":"-100"}]},{"featureType":"poi.sports_complex","elementType":"geometry.fill","stylers":[{"lightness":"5"},{"saturation":"-100"}]},{"featureType":"road.local","elementType":"geometry.fill","stylers":[{"saturation":"-85"},{"lightness":"12"}]}
];
map.setOptions({styles: styles});
}
initialize_map();
}
What I want to do now is on click of say 'West Midlands' #google-wm, it removes all markers currently on the map and then only shows markers where the data-region == 'West Midlands'
How is it possible to do this?
Thanks in advance.
You could do something like that. Code is commented for the parts that I have added/changed.
var markers = [];
var map;
function initialize() {
var myLatLng = new google.maps.LatLng(52, -1);
var mapOptions = {
zoom: 6,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
$('.marker').each(function() {
var location = {
latLng: new google.maps.LatLng(
$(this).data('lat'),
$(this).data('lng')
),
};
var marker = new google.maps.Marker({
map: map,
position: location.latLng,
});
// Register click event
$(this).on('click', function() {
clickMarker($(this).data('region'));
});
// Push marker and region to markers array
markers.push({
'marker': marker,
'region': $(this).data('region')
});
});
}
function clickMarker(region) {
// Loop through markers array
for (var i = 0; i < markers.length; i++) {
// If marker region = selected region, display it
if (markers[i].region === region) {
markers[i].marker.setMap(map);
} else {
// Hide marker from different region
markers[i].marker.setMap(null);
}
}
}
initialize();
#map-canvas {
height: 150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="map-canvas"></div>
<div class="marker" data-lat="52.5" data-lng="-2.1" data-region="West Midlands">Marker 1 - WM</div>
<div class="marker" data-lat="52.6" data-lng="-2.2" data-region="West Midlands">Marker 2 - WM</div>
<div class="marker" data-lat="51.6" data-lng="-0.4" data-region="South East">Marker 3 - SE</div>
<div class="marker" data-lat="51.7" data-lng="-0.5" data-region="South East">Marker 4 - SE</div>
<script src="https://maps.googleapis.com/maps/api/js"></script>

AngularJs- TypeError: Cannot read property 'latitude' of undefined

I want to show Google map with draggable marker in my page . Here is my code:
header.php :
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&sensor=true"></script>
map.js:
app.directive('mapDirective',function(){
return {
templateUrl: 'map.html',
restrict: 'EA',
require: '?ngModel',
scope:{
myModel: '=ngModel'
},
link: function(scope , element, attrs , ngModel){
var mapOptions;
var googleMap;
var searchMarker;
var searchLatLng;
scope.searchLocation = {
latitude: 48.137273,
longitude: 11.575251
};
ngModel.$render = function(){
console.log("hhh");
searchLatLng = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
mapOptions = {
center: searchLatLng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
googleMap = new google.maps.Map(element[0],mapOptions);
searchMarker = new google.maps.Marker({
position: searchLatLng,
map: googleMap,
draggable: true
});
google.maps.event.addListener(searchMarker, 'dragend', function(){
scope.$apply(function(){
scope.myModel.latitude = searchMarker.getPosition().lat();
scope.myModel.longitude = searchMarker.getPosition().lng();
});
}.bind(this));
};
scope.$watch('myModel', function(value){
var myPosition = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
searchMarker.setPosition(myPosition);
}, true);
}
}
});
map.html:
<div>
<div style="display: block; height: 200px; width: 100%; ">
</div>
</div>
index.html:
<map-directive ng-model="testModel"></map-directive>
Actually , I got this error :
TypeError: Cannot read property 'latitude' of undefined
How to solve it?
EDITED:
I change my maps.js:
app.directive('mapDirective',function(){
return {
templateUrl: '/app/user/ngApp/templates/libsView/templates/directives/map.html',
restrict: 'EA',
require: '?ngModel',
scope:{
myModel: '=ngModel'
},
controller: function ($scope) {
$scope.searchLocation = {
latitude: 48.137273,
longitude: 11.575251
};
},
link: function(scope , element, attrs , ngModel){
var mapOptions;
var googleMap;
var searchMarker;
var searchLatLng;
ngModel.$render = function(){
console.log("hhh");
searchLatLng = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
mapOptions = {
center: searchLatLng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
googleMap = new google.maps.Map(element[0],mapOptions);
searchMarker = new google.maps.Marker({
position: searchLatLng,
map: googleMap,
draggable: true
});
google.maps.event.addListener(searchMarker, 'dragend', function(){
scope.$apply(function(){
scope.myModel.latitude = searchMarker.getPosition().lat();
scope.myModel.longitude = searchMarker.getPosition().lng();
});
}.bind(this));
};
scope.$watch('myModel', function(value){
var myPosition = new google.maps.LatLng(scope.myModel.latitude, scope.myModel.longitude);
searchMarker.setPosition(myPosition);
}, true);
}
}
});
But it doesn't change.
myModel variable should contains latitude and longitude values. which should be set from
<map-directive ng-model="testModel"></map-directive>
You need to create a object as below in your controller and assign this object to directive as above html.
var testModel = {
latitude:1.2323,
longitude:2.3434
};

Ember.js with Google Map in view

I'm trying to display an embeded Google Map based on the location of an Event object.
This is the basic app:
App = Em.Application.create();
App.Event = Em.Object.extend({
lat: -33.8665433,
lng: 151.1956316
});
App.EventController = Ember.ObjectController.extend();
App.ApplicationController = Ember.ObjectController.extend();
App.EventView = Ember.View.extend({
templateName: 'event'
});
App.ApplicationView = Ember.View.extend({
templateName: 'application'
});
App.Router = Ember.Router.extend({
enableLogging: true,
root: Ember.Route.extend({
event: Ember.Route.extend({
route: '/',
connectOutlets: function (router) {
router.get('eventController').set('content', App.Event.create());
router.get('applicationController').connectOutlet('event');
}
})
})
});
App.initialize();
With the following templates:
<script type="text/x-handlebars" data-template-name="application">
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="event">
{{lat}} {{lng}}
// Embeded Google Map
</script>
Where would I initialize the map? Additionally, if lat/lang change, how would I catch it and redraw the map?
Working View Code (Modified from sabithpocker's answer)
App.EventView = Ember.View.extend({
templateName: 'event',
map: null,
latitudeBinding: 'controller.content.lat',
longitudeBinding: 'controller.content.lng',
didInsertElement: function () {
var mapOptions = {
center: new google.maps.LatLng(this.get('latitude'), this.get('longitude')),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(this.$().get(0), mapOptions);
this.set('map', map); //save for future updations
this.$().css({ width: "400px", height: "400px" });
},
reRenderMap: function () {
if (this.get('map')) {
var newLoc = new google.maps.LatLng(this.get('latitude'), this.get('longitude'));
this.get('map').setCenter(newLoc);
}
}.observes('latitude', 'longitude') //these are bound to lat/lng of Event
});
Here is a quick idea, not following ember app structure, just an idea.
App.EventView = Ember.View.extend({
templateName: 'event',
map : null,
latitudeBinding : 'App.Event.lat',
longitudeBinding : 'App.Evet.lng',
didInsertElement : function(){
var mapOptions = {
center: new google.maps.LatLng(this.get('latitude'), this.get('longitude')),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(this.$().get(0),mapOptions);
this.set('map',map); //save for future updations
},
reRenderMap : function(){
var newLoc = new google.maps.LatLng(this.get('latitude'), this.get('longitude'));
this.get('map').setCenter(newLoc)
}.observes('latitude','longitude') //these are bound to lat/lng of Event
});
Also I think App.Event should be:
App.Event = Em.Object.extend({
lat: null,
lng: null,
init : function(){
this.set('lat', -33.8665433);
this.set('lng', 151.1956316);
}
});
to avoid the so-called chromosomal mutation in Ember