How to manually reload Google Map with JavaScript - javascript

I am using this piece of code within a Bootstrap template. I am facing issues when loading images within a Bootstrap Tab content pane.
This is the JavaScript code which I use to initialize the Map:
var latlng = new google.maps.LatLng(50, 50);
var myOptions = {
zoom: _zoom,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
}
};
var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'Google Office!'
});
I have not found any method(s) on the internet to manually reload a Google Map instance.
Is this possible and how? Thanks!

Yes, you can 'refresh' a Google Map like this:
google.maps.event.trigger(map, 'resize');
This basically sends a signal to your map to redraw it.

You can refresh with this:
map.panBy(0, 0);

map.setZoom(map.getZoom());
For some reasons, resize trigger did not work for me, and this one worked.

Related

Marker on google map are not shown after map loads

I use jquery.ui.map to load markers on google map, and this is part of my code:
var ip = new google.maps.LatLng(lat, longt);
var mapOptions = {
zoom: 4,
center: ip,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
$('#map_canvas').gmap(mapOptions);
var marker = $('#map_canvas').gmap('addMarker', {
'position': ip,
'bounds': true,
'title':'Your location'
});
While the map is loading I can see the markers, but when everything are loaded the markers disappear and no marker is shown on the map, while we have them, if you take a look at this screen shot you will see that we have the marker (because we can see the marker title when we hover on it) but we cannot see it...
Screen shot: http://s15.postimg.org/5zlsbxwhn/scmap.jpg
The Demo page: http://bev.dev.beardo.co/encontre/
You have to use marker.setMap(map)
I have a nice example on github

Can't add markers to mobile google map

Trying to create a PhoneGap mobile app. Totally new to mobile apps, phonegap, and JS so bear with me. This is where I'm at:
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.710,-73.994), //New york, NY
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions)
};
With that, the map renders in the browser just fine. When adding the following that I've found in a number of tutorials it refuses to render anything. I am trying to do all of this in the googlemap/index.html page that I have created.
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.710,-73.994), //New york, NY
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var LatLng = new google.maps.LatLng(40.710,-73.994)
var marker = new.google.maps.Marker({
position: myLatLng,
title: "hello world!"
});
marker.setMap(map);
};
When I add the following, the entire page refuses to render.
Basically, I'm using the above method to display a google map, and I'm trying to add a pin to the center location. Seems simple enough but proving to be beyond my abilities.
You have a syntax error on this line:
var marker = new.google.maps.Marker({
Notice that there is a dot between the new operator and the class name.
To fix, replace the line above with:
var marker = new google.maps.Marker({
Happy mapping!
PS: The error was reported in the (desktop) browser's console, I'm not sure how you'd see the same on an Android device.

Google Maps buttons not showing

I have a problem with google maps. When i firstly embeeded iframe to my page on ipad i saw buttons like streetview under the map. When i switched maps to be generated from system data, and rendering using javascript api buttons are gone?
Is there a way to show them again, is it just an options that needs to be added to map or buttons only work when using iframe?
var map;
var service;
var infowindow;
function initializeMap() {
var slocation = new google.maps.LatLng(x,y);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: slocation,
zoom: 15
});
var infowindow = new google.maps.InfoWindow({
content: 'example content'
});
var marker = new google.maps.Marker({
position: slocation,
map: map,
title: ' example title'
});
infowindow.open(map, marker);
}
the buttons that i want to achive are http://imageshack.us/photo/my-images/43/buttonsuv.png/
In your code, add this option streetViewControl: true
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: slocation,
zoom: 15,
streetViewControl: true
});
Have a look at this, it summarises all of the street view options.
UPDATE
After the question update, I realise you want your custom image on the street view icon.
That's not possible.
One workaround is to define a custom control on the map, and link events to it with the streetview events.
I've never done it, however you might want to have a look at this

Changing Google Maps V3 Maker Icon the correct way?

The code in example 1 works. I would like to understand the marker.setIcon(); function more (new to JavaScript also).
My question is. In the documentation for Google maps you see something like this for changing the marker.
MarkerImage(url:string, size?:Size, origin?:Point, anchor?:Point, scaledSize?:Size)
How does this relate to what I have done in example 1 for setting up marker Icon, shoud I have done somethign like this instead?
marker = google.maps.MarkerImage({
url: "newIcon.png"
});
marker.setIcon(marker);
and would that have worked?
here is my example
Example 1
function initialize(){
//MAP
var latlng = new google.maps.LatLng('xxx','xxx');
var options = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
//GEOCODER
geocoder = new google.maps.Geocoder();
marker = new google.maps.Marker({
map: map,
draggable: true
});
marker.setPosition(latlng);
marker.setIcon("newIcon.png");
map.setCenter(latlng);
}
You're giving a V2 answer for a V3 question.
There is no GIcon in V3.
var image = new google.maps.MarkerImage("newIcon.png");
Can be used inside your marker as the icon.
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
icon:image
});

Why clustering does not work? (google maps API V3)

I just started using the version 3 of the google maps api, and i am making a simple implementation of clustering, but i cant make it work. Maybe you can see where is my error, and help me making it work:
var map;
function runmap() {
//Prepare cordinates
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
//Prepare other options
var myOptions = {
zoom: 3,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
//,disableDefaultUI: true//Uncoment to disable map controls
};
//Prepare map using de destination id(in the html page) and the options
map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
//Adding markers(Search marker options for more options)
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Zdravo",
icon:"djole.png"//Change the icon of the marker
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(-34.597, 150.744),
map: map,
title:"Zdravo",
icon:"djole.png"//Change the icon of the marker
});
var marker3 = new google.maps.Marker({
position: new google.maps.LatLng(-34.290, 150.444),
map: map,
title:"Zdravo",
icon:"djole.png"//Change the icon of the marker
});
var markers = [];
markers.push(marker);
markers.push(marker2);
markers.push(marker3);
var markerCluster = new MarkerClusterer(map, markers);
}
Update
This is the error i see:
You need the MarkerClusterer or MarkerClustererPlus for Google API version 3. It looks like you are using MarkerClusterer for Google Maps API version 2.
First, I assume you have loaded all the needed libraries (included the one for the clustering) and you get no JS errors. If that is not so and you have doubts, please report all the errors you get and libraries you load.
Try to supply maxZoom level:
var markerCluster = new MarkerClusterer(map, markers, { maxZoom: 10 });
Then when you see the map, zoom out and check if it would group the markers.

Categories

Resources