Make google map responsive with no iframe - css

For example , If you use embedded google map.
You can make your google map responsive with the code below
using iframe (simple iframe)
<div class="ggmap">
<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d212270.5451230493!2d-84.42060395!3d33.7677129!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x88f5045d6993098d%3A0x66fede2f990b630b!2sAtlanta%2C+GA!5e0!3m2!1sen!2sus!4v1396981185525" width="600" height="450" frameborder="0" style="border:0"></iframe>
</div>
.ggmap {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
}
.ggmap iframe,
.ggmapr object,
.ggmap embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
However I use google map by javascript like this below.
no iframe (simple no-iframe)
<div id="map_canvas" style="width:600px; height:450px"></div>
function writeMap(latitude,longitude) {
var latlng = new google.maps.LatLng(latitude,longitude);
var opts = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), opts);
}
$(document).ready( function(){
writeMap(latitude,longitude);
});
In this case, google map could not be responsive.
How can I make this google map responsive??

Make the height and width percentages of the container (and make sure the container has a size).
var latitude = 42.0;
var longitude = -72.0;
function writeMap(latitude,longitude) {
var latlng = new google.maps.LatLng(latitude,longitude);
var opts = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), opts);
}
$(document).ready( function(){
writeMap(latitude,longitude);
});
body, html, #map_canvas {
height: 100%;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" ></div>

Related

How to get Google Maps to show up at 100% in browser with reactjs?

I want to get Google Maps to show up in browser at full screen using reactjs.
My CSS:
html, body, #map_canvas { height: 100%; margin: 0; padding: 0; width:100%; height:100%;}
My component is defined as follows:
class GMap extends React.Component {
render() {
return (<div className="GMap">
<div ref="map_canvas" id="map_canvas">
</div>
</div>)
}
componentDidMount() {
var mapOptions = {
center: { lat: -34.397, lng: 150.644},
zoom: 8
};
var map_canvas = ReactDOM.findDOMNode(this.refs.map_canvas);
this.map = new google.maps.Map(map_canvas,
mapOptions);
}
}
My problem is that nothing shows up. If I specify in the CSS a width:500px and height:500px or some arbitrary value, I can see it, but if I don't, I don't see anything. I want to replicate this in React:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map_canvas { height: 100%; margin: 0; padding: 0;}
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=API_KEY">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: { lat: -34.397, lng: 150.644},
zoom: 8
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
Also, on this line: <div ref="map_canvas" id="map_canvas"> Am I being redundant with ref and id?
I am using ES6, but am open to JSX solutions. I do not want to use any existing "react-google-map-library" for reasons that I want to simply use the existing library.
The problem is that you are setting #map_canvas to be 100% width and height of its parent, GMap which doesn't have a height or width defined as far as I can see.
If you create a fullscreen class, then you can just use it on both elements.
.fullscreen {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
/* extra fullscreen guarantees - you might not need the rest */
position: absolute;
top: 0;
left: 0;
}
You can also remove the redundant id and switch to the more updated version of the ref syntax.
createMap(element) {
var mapOptions = {
center: { lat: -34.397, lng: 150.644},
zoom: 8
};
this.map = new google.maps.Map(element, mapOptions);
}
render() {
return (
<div className="fullscreen GMap">
<div ref={this.createMap} className="fullscreen"></div>
</div>
);
}
In response to your other question — yeah, you can change this stuff conditionally in your render function. In fact, you don't need CSS at all.
render() {
const styles = {
width: window.innerWidth,
height: window.innerHeight
};
// you could also optionally use an alternate css class here.
return (
<div className="GMap">
<div ref={this.createMap} style={styles}></div>
</div>
);
}

Show user exact or approximate location n google maps in asp.net c# in web site

I want to show user current location or approximate location on google maps in my web site using asp.net c#.Is it possible??? If it is kindly help me.I saw many tutorials but fail.
First you need to get google maps API key for your localhost, this link provides details on how to do that:
http://www.aspdotnet-suresh.com/2013/01/generate-google-maps-api-key-for.html
Second in your default.aspx page do this:
script tags:
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
</script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places">
</script>
<script type="text/javascript">
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
alert("Geo Location is not supported on your current browser!");
}
function success(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
var city=position.coords.locality;
var myLatlng = new google.maps.LatLng(lat, long);
var myOptions = {
center: myLatlng,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
position: myLatlng,
title: "lat: " + lat + " long: " + long
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow({ content: "<b>User Address</b><br/> Latitude:"+lat+"<br /> Longitude:"+long+"" });
infowindow.open(map, marker);
}
</script>
in body tag simply include a empty div:
<div id="map_canvas" style="width: 800px; height: 500px"></div>
css:
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
this is the result you should get:
Tutorial: http://www.aspdotnet-suresh.com/2013/01/show-user-current-location-on-google.html

Google Maps doesn't show in IE7

I'm trying to display a simple map but it doesn't work in IE7 (nothing is shown and also the div does't take any place at all).
it works fine in Chrome and Firefox. I haven't tried any other versions of IE
the template is Jade, but it shouldn't be a problem to understand (.class and #id)
div.pull-right(style="width: 450px")
div.well.well-small(style="width: 450px;")
div#map-holder
div.map#map_canvas
this is the map:
script
function initialize() {
var loc = new google.maps.LatLng(#{location.lat}, #{location.lng});
var mapOptions = {
center: loc,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var marker = new google.maps.Marker({
position: loc,
map: map,
animation: google.maps.Animation.DROP
});
}
CSS
#map-holder img {
max-width: none;
}
.map{
width:450px;
height: 400px;
margin-bottom: 20px;
}
​
This is not valid Javascript:
var loc = new google.maps.LatLng(#{location.lat}, #{location.lng});

Resizing google map according to browser resizing

i am working on google map api v3. map is perfectly showing on my page... problem is that when i resize the browser, map fit to its original size when i load the page...
initial state when i load the page
when i resize the browser, map is still sized at initial state size.
[Code]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type='text/javascript'>
var point;
var mrktx;
function mshow()
{
$("#search_content").css("display","");
}
function mhide()
{
$("#search_content").css("display","none");
}
function load() {
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(ShowPosition)
}
else
{
alert("Browser does not support");
setTimeout( function(){ window.location = "../" },500);
}
function ShowPosition(position)
{
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var cwidth = document.getElementsByTagName("body")[0].clientWidth;
var cheight = document.getElementsByTagName("body")[0].clientHeight;
//alert(cwidth + ',' + cheight);
$("#body").css("overflow","hidden");
$("#map_canvas").css("position","absolute");
$("#map_canvas").css("overflow","auto");
$("#map_canvas").css("height",cheight);
$("#map_canvas").css("width",cwidth);
$("#map_canvas").css("z-index","99")
$("#map_canvas").css("top","0");
$("#map_canvas").css("left","0em");
$("#top_nav").css("width",cwidth);
$("#top_nav").css("height","8%");
var latlng = new google.maps.LatLng(lat,lng);
var myOptions = {
zoom: 11,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
$('document').resize(function(){
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );
});
var myMrkrTxt = "";
var infowindow = new google.maps.InfoWindow({ content : myMrkrTxt });
var myMrkr = new google.maps.Marker({position:latlng,map:map});
google.maps.event.addListener(myMrkr,'mouseover', function(){ infowindow.open(map,myMrkrTxt); });
google.maps.event.trigger(map, "resize");
}
}
</script>
<style>
#top_nav
{
position: absolute; z-index: 200; top: 0px; background-color: black;
}
#top_nav h2
{
color: white;
}
body, html {
height: 100%;
width: 100%;
}
</style>
</head>
<body onload='load()'>
<div id="map_canvas"></div>
</body>
</html>
i guess you have to resize your map_canvas as well.
so just add this to your resize()
//its maybe better to attach this handler to the window instead of the document
$(window).resize(function(){
$('#map_canvas').css("height",$(window).height());
$('#map_canvas').css("width",$(window).width());
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );
});
so you have track of the resizing of your browserwindow :)
Same things can be done using only CSS too. I'll put an example below, use this if you like.
.google-maps {
position: relative;
padding-bottom: 75%;
height: 0;
overflow: hidden;
}
.google-maps iframe {
position: absolute;
top: 0;
left: 0;
width: 100% !important;
height: 100% !important;
}
<div class="google-maps">
<iframe src="https://www.google.com/maps/yourmapsblah" width="765" height="500" frameborder="3" style="border:0" allowfullscreen></iframe>
</div>

Map zoom controls not displaying correctly [duplicate]

This question already has answers here:
Google Map Infowindow not showing properly
(6 answers)
Closed 8 years ago.
I have created a simple map using API3. However, the zoom controls on the top left look "Squashed" - a they are not displaying properly. The rest of the map is fine. The weird thing is that I have used the same method as for previous sites, where things work well.
Here's some code:
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var marker;
var markersArray=[];
var html; //to create urls
var directionsVisible = new Boolean();
directionsVisible = false;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var orchards = new google.maps.LatLng(52.512805,-2.76007);
var myOptions = {
zoom:14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: orchards,
panControl: false
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
addMarker(orchards);
}
The issue should be because of universal img { max-width: 100%; }
Try this one in to your css
.gmnoprint img {
max-width: none;
}
Initially I face same problem, then later on I realize
google.maps.event.addDomListener(window, 'load', initialize);
play very important role.
I added it and for me it works.
Checkout following code.
CSS code
<style>
#divmap {
height: 300px;
width:100%;
margin: 10px;
padding: 10px;
}
.gm-style img { max-width: none; }
.gm-style label { width: auto; display: inline; }
</style>
For JS
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"></script>
<script>
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(21,78),
panControl:true,
zoomControl:true,
mapTypeControl:true,
scaleControl:true,
streetViewControl:true,
overviewMapControl:true,
rotateControl:true
}
var map = new google.maps.Map(document.getElementById("divmap"),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
and final html
<div id="divmap"></div>
if you are using the Dreamweaver fluid grid feature your default set up should look like this:
img, object, embed, video {
max-width: 100%;
}
/* IE 6 does not support max-width so default to width 100% */
.ie6 img {
width:100%;
}
Try this:
object, embed, video {
max-width: 100%;
}
#map_canvas img { max-width: none; }
/* IE 6 does not support max-width so default to width 100% */
.ie6 img {
width:100%;
}
just replace the code as it is.

Resources