Google maps displaying jibberish - javascript

my map is not rendering correctly.
I have never experienced this yet (notice the icons on the right buttons being distorted). The code used to generate the map is extremely simple:
<script>
function initMap() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
center: {lat: 43.828430, lng: 18.3445995},
zoom: 18
});
new google.maps.Marker({
position: {lat: 43.828430, lng: 18.3445995},
map: map
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA9zhNUfid1PgR6FZ-g8xTp_NCqV3JvsHM&callback=initMap" async defer></script>
Any ideas?

Idk why, but when I tried your code, it worked but the map was also gray. When I pointed style="width:500px;height:500px" map become working

Okay so the problem was that I had a bad CSS rule that was interfering with google's rendering.
For reference it was 100% max width on img.

Related

Google Marker animation change

It there a way to add another animation on Marker which is different than DROP and BOUNCE. I would like to enlarge the marker. Thanks in advance!
<Marker
position={position}
animation={window.google.maps.Animation.DROP}
/>
Since my comment seems to resolve your issue, I'll post this as answer so that others can also see that your question already had a resolution and might help them in the future. Here's the answer:
Google Maps marker animations are only bounce and drop. If you want to enlarge a marker at runtime, you can use custom Icons instead(a small and a big one). Then use setTimeout between them. Here's a sample JavaScript fiddle.
function initMap() {
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({
icon: 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_orange.png',
position: myLatLng,
map: map,
title: 'Hello World!'
});
setTimeout(function(){ marker.setIcon("http://maps.google.com/mapfiles/kml/paddle/orange-blank.png") }, 3000);
}

Can Google Maps API v.3 work for Mars?

I am playing with writing a little javascript solution that would take place on mars. I would love to have the output result in a google map of a lat and long on Mars. Any thoughts? Can the following concept BE called for a location on the red planet?
`map = new google.maps.Map();`
I see stuff in the API that indicates this at lease WAS doable in v.2, but I don't know if that survived, and can't seem to scare anything relevant up to tell me if this is a possibility or a dead end.
For Earth, this looks straight forward enough.
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
var markerTest = {lat: -34.397, lng: 150.644};
var marker = new google.maps.Marker({
position: markerTest,
map: map
});
}
Here's an Fiddle example: https://jsfiddle.net/chakra5/qx3r12xk/
Many thanks in advance for any and all brainstorming.

Google maps API will not load grey box

This is the first time I've used a google api and I followed the instructions exactly but the map won't load just the grey box that I started with, please help.
<div class="map">
</div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>
function apimap() {
var uluru = {lat: -73.993439, lng: 40.750545};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
It seems there's an issue regarding your implementation. You are using apimap() as your init function while in your load request's callback, you are using initMap(). You should replace initMap() with apimap() or vice-versa. With this, your map will work.
I've replicated your issue, the location is in Antartica. You can check the demo below.
var map;
function apimap() {
var uluru = {lat: -73.993439, lng: 40.750545};
map = new google.maps.Map(document.getElementById('map'), {
center: uluru,
zoom: 4
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCzjs-bUR6iIl8yGLr60p6-zbdFtRpuXTQ&callback=apimap"
async defer></script>
And also, please refrain from posting your Google Maps API key unless it is restricted to prevent abuse and to avoid your API key being used by unauthorized applications. To learn more about API restrictions, you can check Restricting an API key.
Hope it could help and good luck on your coding!

Google Maps Marker in the wrong position

I am struggling to understand this.
My code is below along with a link to a test page showing the map
with marker in the wrong place.
I have set the lat and lng consistent with
https://www.google.co.uk/maps/place/Dakota+Hotel+Eurocentral/#55.8353173,-3.9854019,17z/data=!3m1!4b1!4m5!3m4!1s0x4888441f8bb5be07:0x6444cfc231481afe!8m2!3d55.8353173!4d-3.9832132
As you can see from the link they are 55.8353173 and -3.9854019
However when I include them in my code below - the marker appears in the wrong place.
See http://craigfagan.com/queenslie/testmap.html
My code is below.
Any help would be greatly appreciate on this - Scratching my head wondering what I am doing wrong?
Thanks in advance for any advice..
<div id="map"></div>
<script type="text/javascript">
function initMap() {
var mapOptions = {
zoom: 17,
scrollwheel: false,
center: new google.maps.LatLng(55.8353173,-3.9854019),
styles:
[{"featureType":"water","elementType":"geometry.fill","stylers":
[{"color":"#d3d3d3"}]},{"featureType":"transit","stylers":
[{"color":"#808080"},{"visibility":"off"}]},
{"featureType":"road.highway","elementType":"geometry.stroke",
"stylers":
[{"visibility":"on"},{"color":"#b3b3b3"}]},
{"featureType":"road.highway","elementType":"geometry.fill","stylers":
[{"color":"#ffffff"}]},
{"featureType":"road.local","elementType":"geometry.fill","stylers":
[{"visibility":"on"},{"color":"#ffffff"},{"weight":1.8}]},
{"featureType":"road.local","elementType":"geometry.stroke","stylers":
[{"color":"#d7d7d7"}]},
{"featureType":"poi","elementType":"geometry.fill","stylers":
[{"visibility":"on"},{"color":"#ebebeb"}]},
{"featureType":"administrative","elementType":"geometry","stylers":
[{"color":"#a7a7a7"}]},
{"featureType":"road.arterial","elementType":"geometry.fill","stylers":
[{"color":"#ffffff"}]},
{"featureType":"road.arterial","elementType":"geometry.fill","stylers":
[{"color":"#ffffff"}]},
{"featureType":"landscape","elementType":"geometry.fill","stylers":
[{"visibility":"on"},{"color":"#efefef"}]},
{"featureType":"road","elementType":"labels.text.fill","stylers":
[{"color":"#696969"}]},
{"featureType":"administrative","elementType":"labels.text.fill",
"stylers":
[{"visibility":"on"},{"color":"#737373"}]},
{"featureType":"poi","elementType":"labels.icon","stylers":
[{"visibility":"off"}]},
{"featureType":"poi","elementType":"labels","stylers":
[{"visibility":"off"}]},
{"featureType":"road.arterial","elementType":"geometry.stroke",
"stylers":
[{"color":"#d6d6d6"}]},
{"featureType":"road","elementType":"labels.icon","stylers":
[{"visibility":"off"}]},{},
{"featureType":"poi","elementType":"geometry.fill","stylers":
[{"color":"#dadada"}]}]
};
var mapElement = document.getElementById('map');
var map = new google.maps.Map(mapElement, mapOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(55.8353173,-3.9854019),
map: map,
title: 'Dakota'
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?
key=AIzaSyDJRdPso7qlkubRB66DafdHiBClCZoGGsI&callback=initMap" async
defer></script>
The coordinates in the link after the # sign are the center of the map, not the marker.
To get the center of the map in Google Maps, right click on the icon of the place, click "what's here", it will give you the coordinates (55.835326,-3.983235 for that example).
If I use the Places API (for "Dakota Hotel Eurocentral"), I get (55.8353173,-3.983213200000023), which is better.

Changing javascript in google maps doesn't update it?

I am trying to make an AJAX map Google map, where the place markers update but not the map.
I have an ajax setup with a settimeout function, which returns new javascript. When the new javascript is loaded into my HTML, the changes do not reflect on my google map.
When I go into the firefox inspector and try and manipulate the javascript, (to try and change marker GPS coordinates), nothing happens to the map, and the points are still the same. Why does that not affect the map?
I have looked at several links to try and help me, but none of them explain the logic behind the javascript.
My PHP script returns javascript in plain text. But when these get
added to the HTML, The google map does not change and looks like it
needs to be re initialized before it recognizes new javascript?
Can someone explain how I should update the javascript, so the map re-centers on the newest marker and places the newest marker without refreshing the page / re initializing the map?
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: {lat: -20.530637, lng: 46.450046}
});
// Create an array of alphabetical characters used to label the markers.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Add some markers to the map.
// Note: The code uses the JavaScript Array.prototype.map() method to
// create an array of markers based on a given "locations" array.
// The map() method here has nothing to do with the Google Maps API.
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
var locations = [
new google.maps.LatLng("-21.530637,46.450046),
new google.maps.LatLng("-22.530637,46.450046),
]
</script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
</script>
<div class="result"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function refresh_div() {
jQuery.ajax({
url:'content.php',
type:'POST',
success:function(results) {
locations=results;
jQuery(".result").html(results);
}
});
}
t = setInterval(refresh_div,5000);
</script>
My "content.php" file. returns more google maps LatLng markers in plain text.
so PHP output is:
new google.maps.LatLng("-23.530637,46.450046"),
new google.maps.LatLng("-24.530637,46.450046"),
new google.maps.LatLng("-25.530637,46.450046"),
new google.maps.LatLng("-26.530637,46.450046")
I would like to summary the behavior of your ajax map:
Initialize map (probably with a set of predefined locations)
Repeatedly call content.php to retrieve new update of markers
Draw new markers to your map
The problem is that your code that handles ajax result doesn't do any map manipulation. In fact, it has to update the variable locations with new location set. Then create new markers with updated list of locations, then call MarkerCluster to apply them.
I created a fiddle to demonstrate it: https://jsfiddle.net/anhhnt/paffzadz/1/
What I did is extract the marker creating part from initMap, so that it can be called multiple times after locations is updated.
function updateMarker () {
// Create an array of alphabetical characters used to label the markers.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Add some markers to the map.
// Note: The code uses the JavaScript Array.prototype.map() method to
// create an array of markers based on a given "locations" array.
// The map() method here has nothing to do with the Google Maps API.
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
};
function initMap() {
window.map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: -28.024, lng: 140.887}
});
updateMarker(map);
};
var locations = [
{lat: -31.563910, lng: 147.154312},
{lat: -33.718234, lng: 150.363181},
{lat: -33.727111, lng: 150.371124}
]
function refresh_div() {
jQuery.ajax({
url:'/echo/json/',
type:'POST',
data: {
json: JSON.stringify([
{lat: -42.735258, lng: 147.438000},
{lat: -43.999792, lng: 170.463352}
])
},
success:function(results) {
locations.concat(results);
updateMarker();
}
});
}
t = setInterval(refresh_div,5000);
Hope it helps.
Perhaps you have simple syntax error in your code - missing closing double quote:
new google.maps.LatLng("-23.530637,46.450046"),
...
..or better - remove first double quote:
new google.maps.LatLng(-23.530637,46.450046),
...

Categories

Resources