I'am a new Backbone user and started to implement Google Maps in it. I set this up in de View object. I works all fine, but when i go to another page (template with the navigator object) and go back to the page with Google Maps, the current position dont market on the Google Map.
Below my code from my View
DFM.MapView = Backbone.View.extend({
el: $("#content"),
template: $("#rankings_template").html(),
// Initialiseren
initialize: function(){
self = this;
this.render();
var location_destiny = new google.maps.LatLng(52.476716,4.799623);
var map = new google.maps.Map(document.getElementById('google_maps'), {
center: location_destiny,
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
panControl: false,
zoomControl: false,
mapTypeControl: false,
scaleControl: false,
streetViewControl: false,
overviewMapControl: false
});
if(navigator.geolocation){
navigator.geolocation.watchPosition(update_user_location, error_handler, {
enableHighAccuracy: true,
frequency: 3000
});
} else {
console.error('Geolocation: 0 - Geotracking not available');
}
// Update user location
function update_user_location(position){
location_user = new google.maps.LatLng(52.474716,4.798623);
var marker_user = new google.maps.Marker({
title: 'Gebruiker',
position: location_user,
map: map,
draggable: true,
});
}
// Error handler
function error_handler(error){
console.error('Geolocation: ' + error.code + ' - ' + error.message);
}
var marker_destiny = new google.maps.Marker({
title: 'Bestemming',
position: location_destiny,
map: map,
draggable: true,
});
},
render: function(){
this.$el.html(_.template(this.template));
return this;
}
});
Not sure if I got the question right but for me it helped calling google.maps.event.trigger(map,'resize'); every time after triggering the map route.
PS: a bit late, but maybe it helps someone in the future
Related
I am working on an ionic app in which i need to show google map to display location. I need to disable the satellite and street view options in the map, but didnt get any suitable method.
Also the map is creating issues, sometimes it shows on the screen and sometime it didnt.
This is the code i am trying in controller
$scope.addLoc = function(){
$cordovaGeolocation.getCurrentPosition(options).then(function(position){
$scope.lat = position.coords.latitude;
$scope.long = position.coords.longitude;
var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+$scope.lat+","+$scope.long+"&sensor=true";
$http.get(url)
.then(function(res) {
$rootScope.address = res.data.results[0].formatted_address;
console.log($rootScope.address);
}, function(error) {
console.log( error);
});
console.log(position);
var latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var mapOptions = {
center: latLng,
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
$scope.map = new google.maps.Map(document.getElementById("map"), mapOptions);
$scope.marker = new google.maps.Marker({
position: new google.maps.LatLng($scope.lat, $scope.long),
map: $scope.map,
title: 'Drag me!'
}, function(err) {
console.err(err);
});
}, function(error){
console.log("Could not get location");
});
}
You can try this
var mapOptions = {
center: latLng,
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false,
disableDefaultUI: true
};
Doc: https://developers.google.com/maps/documentation/javascript/controls#DefaultUI
When you enable the map and passes the options to it, you have the chance to specify a mapTypeControlOptions. These have an Array that specifies what kind of maptype's you will allow the user to be able to see. It can be seen here http://code.google.com/apis/maps/documentation/javascript/reference.html#MapTypeControlOptions.
If you don't want the user to have any options as to the maptypes, you can also specify that by setting the maps mapTypeControl to false.
var myOptions = {
zoom: 2,
center: **Your LatLng object**,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.HYBRID]
}, // hereĀ“s the array of controls
disableDefaultUI: true, // a way to quickly hide all controls
mapTypeControl: true,
scaleControl: true,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); // displays in <article id="map_canvas"></article>
//map.mapTypeControl = false; // OPTIONAL: hides the map control
You can hide them via css
But this not a proper way..This may not work in the future..
.gm-style-mtc {
display: none;
}
Hi guys i'm working with this code below , but i don't know is is possible
and how i can do to add more one marker and set other DIV to show this position.
Anyone know what can i do or give me a tip
Its a cordova application on visual studio
angular.module('app', ['onsen']).
directive('myMap', function () {
// directive link function
var link = function (scope, element, attrs) {
var map, infoWindow;
var markers = [];
// map config
var mapOptions = {
center: new google.maps.LatLng(-29.3130754, -50.8542323),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
// init the map
function initMap() {
if (map === void 0) {
map = new google.maps.Map(element[0], mapOptions);
}
}
// place a marker
function setMarker(map, position, title, content) {
var marker;
var markerOptions = {
position: position,
map: map,
title: title,
icon: 'images/Maps/blue-dot.png'
};
marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array
google.maps.event.addListener(marker, 'click', function () {
// close window if not undefined
if (infoWindow !== void 0) {
infoWindow.close();
}
// create new window
var infoWindowOptions = {
content: content
};
infoWindow = new google.maps.InfoWindow(infoWindowOptions);
infoWindow.open(map, marker);
});
}
// show the map and place some markers
initMap();
setMarker(map, new google.maps.LatLng(-29.3130754, -50.8542323), 'adress');
};
return {
restrict: 'A',
template: '<div id="gmaps"></div>',
replace: true,
link: link
};
});
Here is a way to add multiple markers on a google maps , u just need an array which contains your required latitude longitude values .
var myLatLng = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
var myLatLng1={lat: -25.163, lng: 131.644};
var marker2 = new google.maps.Marker({
position: myLatLng1,
map: map,
title: 'Hello World!'
});
you could also have an array which contains the array element conataining the latutude and longitude values .
for (var i = 0; i < locations.length; i++) { //Your location contains the lat long position
var marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
title: 'Hello World!'
});
After following a tutorial demo regarding google map and angularjs directive, I have the sample code with:
HTML
<map-geo-location height="400" width="600"></map-geo-location>
And the directive JS:
function link(scope, elem, attrs) {
status = angular.element(document.getElementById('status'));
mapContainer = angular.element(document.getElementById('map'));
mapContainer.attr('style', 'height:' + scope.height +
'px;width:' + scope.width + 'px');
$window.navigator.geolocation.getCurrentPosition(mapLocation, geoError);
}
function mapLocation(pos) {
status.html('Found your location! Longitude: ' + pos.coords.longitude +
' Latitude: ' + pos.coords.latitude);
var latlng = new google.maps.LatLng(pos.coords.latitude,
pos.coords.longitude);
var options = {
zoom: 15,
center: latlng,
mapTypeControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(mapContainer[0], options);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Your location"
});
}
return {
scope: {
height: '#',
width: '#'
},
link: link,
template: template
};
However, I noticed that, if I do NOT want to load current geoLocation and just want to load map with some given coordinates, the map is not loaded.
Why is that? Is it caused by any aync call? If so, could to address it?
Thanks a lot!
I figured it out, just remove the geo get location statement, and ensure the following is invoked directly inside the link() function.
I have a backbone.js model. I want to load google maps API javascript from that model's method. I don't want to load the script in head of the document.
I can use $.getScript() to load the js & then display the map? But when I specify the callback function
http://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=TRUE&callback=initialize
I always get an error because I dont want to define a global js function initialize(). I want the callback method to be a same backbone.js model's method.
App = Backbone.Model.extend({
construct : function(),
displayMap : function()
{
$.getScript("http://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=TRU&callback=initialize", function(data, textStatus, jqxhr) {
console.log('Load was performed.');
});
},
initialize: function()
{
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
});
var App = new App();
App.displayMap();
Try this structure for map model:
Map = Backbone.Model.extend({
defaults: {
id: '', currentLatLng: {}, mapOptions: {}, map: {},
position: {}, zoom: 13, maxZoom: 16, minZoom: 12
},
initMap: function(position){
this.set('position', position);
var currentLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
this.set('currentLatLng', currentLatLng);
var mapOptions = {
zoom: this.get('zoom'),
minZoom: this.get('minZoom'),
maxZoom: this.get('maxZoom'),
center: currentLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
this.set('mapOptions', mapOptions);
}
});
This code for view:
MapView = Backbone.View.extend({
defaults:{
region: 'us', language: 'en'
},
id: 'gmaps-container',
className: 'gmaps_container',
initialize: function(){
var url = "http://maps.googleapis.com/maps/api/js?key=key_here&sensor=false";
$.ajax({
url: url,
dataType: "script",
async: false,
success: function(){
console.log('script loaded');
}
});
this.model.set('map', new google.maps.Map(this.el, this.model.get('mapOptions')));
},
render: function(){
console.log('init map');
$('#' + this.id).replaceWith(this.el);
return this;
}
});
And just initialize model and view in main script:
var map = new Map({zoom: 8, maxZoom: 18, minZoom: 8});
map.initMap({coords: {latitude: -34.397, longitude: 150.644}});
var mapView = new MapView({model: map});
mapView.render();
Also you need simple html container:
<div id="gmaps-container" class="gmaps_container"></div>
Note: set width and height for container.
I have found example if someone need however I need to get first coordinates from address so later can adjust it. Someone knows how to do it?
var geocoder = new google.maps.Geocoder();
function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
updateMarkerAddress(responses[0].formatted_address);
} else {
updateMarkerAddress('Cannot determine address at this location.');
}
});
}
function updateMarkerStatus(str) {
document.getElementById('markerStatus').innerHTML = str;
}
function updateMarkerPosition(latLng) {
document.getElementById('info').innerHTML = [
latLng.lat(),
latLng.lng()
].join(', ');
}
function updateMarkerAddress(str) {
document.getElementById('address').innerHTML = str;
}
function initialize() {
var latLng = new google.maps.LatLng(1.54232,-1.4353423);
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 8,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map: map,
draggable: true
});
// Update current position info.
updateMarkerPosition(latLng);
geocodePosition(latLng);
// Add dragging event listeners.
google.maps.event.addListener(marker, 'dragstart', function() {
updateMarkerAddress('Dragging...');
});
google.maps.event.addListener(marker, 'drag', function() {
updateMarkerStatus('Dragging...');
updateMarkerPosition(marker.getPosition());
});
google.maps.event.addListener(marker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
});
// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
if your browser support the geolocation you want get coordinate with this function :
navigator.geolocation.getCurrentPosition(function(position) {
geolocationCoordinates = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
//position.coords.latitude = latitude coordonates;
//position.coords.longitude = longitude coordonates;
}
after, you can create a new map whith this coordonates.
This code is functional with all browser supporting the geolocation.