google map is not loading completely - google-maps

google maps not working i have already done this code. but when i am using the same code in another project its not working. the lat long values are fine.
<div class="row">
<div class="col-md-12">
<div id="googleMap" style="height: 354px; width:200px;"></div>
</div>
</div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWT14OcdohKHZ1i-BmHEETzm6DUskY8Cg" type="text/javascript"></script>
<script>
var lat = '<?php echo $lat?>';
var long = '<?php echo $longi?>';
var myCenter = new google.maps.LatLng(lat, long);
var marker;
function initialize()
{
var mapProp = {
center: myCenter,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var marker = new google.maps.Marker({
position: myCenter,
animation: google.maps.Animation.BOUNCE
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
});
</script>

The posted code works if I remove the async defer from the API include:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWT14OcdohKHZ1i-BmHEETzm6DUskY8Cg" type="text/javascript"></script>
(you should either remove them, or add a &callback=initialize to the include and remove the addDomListener that runs initialize on load)
BTW - you also have a syntax error, an extra });)
var lat = 42;
var long = -72;
var myCenter = new google.maps.LatLng(lat, long);
var marker;
function initialize() {
var mapProp = {
center: myCenter,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var marker = new google.maps.Marker({
position: myCenter,
animation: google.maps.Animation.BOUNCE
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWT14OcdohKHZ1i-BmHEETzm6DUskY8Cg" type="text/javascript"></script>
<div class="row">
<div class="col-md-12">
<div id="googleMap" style="height: 354px; width:200px;"></div>
</div>
</div>

Related

Add second marker on google maps javascript code

This is my code and i want to add a second marker on it. I'm not experienced on javascript and i couldn't figure out a way to do it. I'ld love some explain also!
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(XX.XXXXX, YY.YYYYYY),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
var myLatlng = new google.maps.LatLng(XX.XXXXX, YY.YYYYYY);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "'. $xmladd .'"
});
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addDomListener(window, "resize", function () {
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
});
</script>
Duplicate the code for the first marker, changing the position appropriately.
code snippet:
html,
body,
#map-canvas {
height: 100%;
width: 100%;
}
<div id="map-canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.7127837, -74.0059413),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
var myLatlng = new google.maps.LatLng(40.7127837, -74.0059413);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "'. $xmladd .'"
});
var myLatlng2 = new google.maps.LatLng(40.735657, -74.1723667);
var marker2 = new google.maps.Marker({
position: myLatlng2,
map: map,
title: "'. $xmladd2 .'"
});
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addDomListener(window, "resize", function() {
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
});
</script>

Google Maps API v3 - Can't create position marker

Trying to do a simple position marker on a Google map imbed. Everything looks right, but not working.
<link rel="stylesheet" type="text/css" href="default.css"/>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(40.0501322,-82.914233),
zoom: 13,
mapTypeId: google.maps.MapTypeId.HYBRID
}
var map = new google.maps.Map(mapCanvas, mapOptions)
}
google.maps.event.addDomListener(window, 'load', initialize);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(40.0496714,-82.9121331),
map: map
});
</script></head>
The map is loading fine, but no marker is found.
You need to create the marker inside the initialize function (where the map variable exists).
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(40.0501322, -82.914233),
zoom: 13,
mapTypeId: google.maps.MapTypeId.HYBRID
}
var map = new google.maps.Map(mapCanvas, mapOptions)
var marker = new google.maps.Marker({
position: new google.maps.LatLng(40.0496714, -82.9121331),
map: map
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
height: 100%;
width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
Yes, you need to initialize the marker object inside the initialize() method of the code. One way is how geocodezip has demoed. Here is one more way if you want to write modular code.
<script type="text/javascript">
// Standard google maps function
function initialize() {
var myLatlng = new google.maps.LatLng(40.0501322, -82.914233);
var myOptions = {
zoom: 12,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
TestMarker();
}
// Function for adding a marker to the page.
function addMarker(location) {
marker = new google.maps.Marker({
position: location,
map: map
});
}
// Testing the addMarker function
function TestMarker() {
GoldenGatePark = new google.maps.LatLng(37.7699298, -122.4469157);
addMarker(GoldenGatePark);
}
</script>
Here you have added a marker in the TestMarker function and the TestMarker function is called in the initialize() method. You can just change the values of LatLng in the TestMarker function code.

Google Map not displayed . Error InvalidValueError: setMap: not an instance of Map; and not an instance of StreetViewPanorama

I am using google maps js to show Map and load the markers on it . It was working fine . But suddenly got following error
"InvalidValueError: setMap: not an instance of Map; and not an instance of StreetViewPanorama"
Here is the code i am using
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
$(document ).ready(function(){
var lax;
var lox;
var zlevel = Math.round(14-Math.log(20)/Math.LN2);
var userLatLng = new google.maps.LatLng(lax, lox);
var myOptions = {
zoom : zlevel ,
center : userLatLng,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
// Draw the map
var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
var userLatLng = new google.maps.LatLng(lat1, lon1);
var marker = new google.maps.Marker({
map: mapObject,
icon: './images/icons/regular.png',
position: userLatLng,
title:name,
url: './index.php',
});
var userLatLng = new google.maps.LatLng(lat2, lon2);
var marker = new google.maps.Marker({
map: mapObject,
icon: './images/icons/regular.png',
position: userLatLng,
title:name,
url: './index.php',
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = this.url; //changed from markers[i] to this[i]
});
marker.setMap(map);
});
<div id="map"></div>
Can anybody hemlp me what is the issue ? what is the fix ?
Thanks
remove this line:
marker.setMap(map);
map is not a google.maps.Map (it's the div )
You don't need this line at all, because the map-property of marker already has been set.
What you need are the variables(lat,lox,lat1,lon1,lat2,lon2), without them you will not get the map and the markers
use this way:-
$(document).ready(function(){
var gtpCoOrdsGTP = new google.maps.LatLng(12.97283,77.72024);
var mapPropGTP = {
center:gtpCoOrdsGTP,
zoom:18,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var mapGTP=new google.maps.Map(document.getElementById("googleMapGTP"),mapPropGTP);
var markerGTP = new google.maps.Marker({
position: gtpCoOrdsGTP,
map: mapGTP,
title: "my title"
});
});
In your code, you initialized map as below:
// Draw the map
var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
And you set map in marker as below
marker.setMap(map);
It should be
marker.setMap(mapObject);
below code will work perfaclty.
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyBrzfgGntjIVECT_T8yHY5kf3dwH6ltz6c"></script>
<script type="text/javascript">
$(document ).ready(function(){
var lax;
var lox;
var zlevel = Math.round(14-Math.log(20)/Math.LN2);
var userLatLng = new google.maps.LatLng(lax, lox);
var myOptions = {
zoom : zlevel ,
center : userLatLng,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
// Draw the map
var mapObject = new google.maps.Map(document.getElementById("myMap"), myOptions);
var userLatLng = new google.maps.LatLng(23.022505, 72.571362);
var marker = new google.maps.Marker({
map: mapObject,
icon: 'https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi2.png',
position: userLatLng,
title:"one",
url: './index.php',
});
var userLatLng = new google.maps.LatLng(21.170240, 72.831061);
var marker = new google.maps.Marker({ map: mapObject,
icon: 'https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi2.png',
position: userLatLng,
title:"two",
url: './index.php',
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = this.url; //changed from markers[i] to this[i]
});
marker.setMap(mapObject);
});
</script>
</head>
<body>
<div id="myMap" style="height:400px;width:400px">
</div>
</body>
</html>

How to point out a location in google maps?

Hi, i am trying to point out the location of my company. It goes to the right location but the pointer is missing. I tried alot of things but it still doesn't work. I am a beginning programmer and i need some help. Thank you for your time.
This is my code:
<jsp:include page="header.jsp">
<jsp:param name="subTitle" value="AutoTotaalDiensten - Locatie info" />
</jsp:include>
</head>
<%# include file="menu.html"%>
Locatie
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<style>
#map_canvas {
height: 400px;
}
<div id="map_canvas"></div>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
function initialize() {
var map_canvas = document.getElementById('map_canvas');
var map_options = {
var mapPin = " http://www.google.com/mapfiles/marker.png";
var Marker = new google.maps.Marker({
center : new google.maps.LatLng(52.124707, 5.088196),
zoom : 13,
mapTypeId : google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(map_canvas, map_options)
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<%# include file="footer.html"%>
Call Marker.setMap(map) after you initialize the map.
function initialize() {
var map_canvas = document.getElementById('map_canvas');
var map_options = {
center : new google.maps.LatLng(52.124707, 5.088196),
zoom : 13,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
var mapPin = " http://www.google.com/mapfiles/marker.png";
var Marker = new google.maps.Marker({
position : map_options.center,
});
var map = new google.maps.Map(map_canvas, map_options)
Marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
working fiddle
Or create it after you create and initailize the map and set its "map" property.
function initialize() {
var map_canvas = document.getElementById('map_canvas');
var map_options = {
center : new google.maps.LatLng(52.124707, 5.088196),
zoom : 13,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
var mapPin = " http://www.google.com/mapfiles/marker.png";
var map = new google.maps.Map(map_canvas, map_options)
var Marker = new google.maps.Marker({
map: map,
position: map.getCenter()
});
}
google.maps.event.addDomListener(window, 'load', initialize);
working fiddle

Google maps myoptions will not work

I'm trying to customize a map of Mass..to show 10 districts. I've added the link to the map created in "my places", but I can't get the map to center, zoom or title.. The center LatLng is Worcester.. nothing works
Help !!
Thanks
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dist-layer</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"> </script>
<script>
function initialize() {
// var myLatlng = new google.maps.LatLng(42.268843,71.803774);
// zoomControl:false,
// zoom: 18,
// Title: "Mass Districts",
// center: myLatlng
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var mapOptions = {};
var kmlLayer = new google.maps.KmlLayer({
url: "http://mapsengine.google.com/map/kml? mid=zHTaYadv8Mrs.kHqpg6p0mrlk&lid=zHTaYadv8Mrs.khlpCJFyVigQ",
suppressInfoWindows: true,
Map:map
});
google.maps.event.addListener(kmlLayer, 'click', function(kmlEvent) {
var text = kmlEvent.featureData.description;
showInContentWindow(text);
});
function showInContentWindow(text) {
var sidediv = document.getElementById('content-window');
sidediv.innerHTML = text;
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas" style="width:40%; height:472px; float:left;"> </div>
<div id="content-window" style="min-width:15%; max-width:28%; height:430px; float:left; border: #0ff 5px double;padding: 10px;"> </div>
</body>
</html>
Set preserveViewport to true, otherwise the KmlLayer will zoom to fit its contents.
var kmlLayer = new google.maps.KmlLayer({
url: "http://mapsengine.google.com/map/kml?mid=zHTaYadv8Mrs.kHqpg6p0mrlk&lid=zHTaYadv8Mrs.khlpCJFyVigQ",
suppressInfoWindows: true,
preserveViewport:true,
map: map
});
function initialize() {
var myLatlng = new google.maps.LatLng(42.2625932, -71.8022934);
// zoomControl:false,
// zoom: 18,
// Title: "Mass Districts",
// center: myLatlng
var mapOptions = {
zoomControl: false,
zoom: 11,
title: "Mass Districts",
center: myLatlng
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var kmlLayer = new google.maps.KmlLayer({
url: "http://mapsengine.google.com/map/kml?mid=zHTaYadv8Mrs.kHqpg6p0mrlk&lid=zHTaYadv8Mrs.khlpCJFyVigQ",
suppressInfoWindows: true,
preserveViewport:true,
map: map
});
google.maps.event.addListener(kmlLayer, 'click', function (kmlEvent) {
var text = kmlEvent.featureData.description;
showInContentWindow(text);
});
function showInContentWindow(text) {
var sidediv = document.getElementById('content-window');
sidediv.innerHTML = text;
}
}
google.maps.event.addDomListener(window, 'load', initialize);
fiddle