I need to implement map and custom marker as shown here , here is the map implementation html http://jsfiddle.net/4a87k/ But no idea how to add custom css as marker.
<head>
<style>
/* 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;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
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({
position: myLatLng,
map: map,
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?sensor=false&callback=initMap">
</script>
</body>
The documentation for customising markers is very good, find it here.
And a simple example is to link to a png file directly for the marker, but you can do more with this with Custom Markers in the api:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: -33, lng: 151}
});
var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
var beachMarker = new google.maps.Marker({
position: {lat: -33.890, lng: 151.274},
map: map,
icon: image
});
}
Related
I´m trying to make a simple code using Google maps API (I'm new at this), the idea is to get the latitude and longitude from JSON variable direccion to the var uluru.
I have the following code:
Where am I wrong?
Thanks in advance.
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 100%;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap() {
var direccion=[
{
lugar:'CICESE',
lat:31.8675375,
lng: -116.6686867
},
{
lugar: 'Casa',
lat:31.8915163,
lng:-116.6879557
}
];
var uluru = {lat: direccion.lat, lng: direccion.lng};//{lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDgMyVeh8DwebGfZHjiNjyle4xFe9pKSdc&callback=initMap">
</script>
</body>
</html>
Your direction is json array, you need to access by index as below:
var uluru = {lat: direccion[0].lat, lng: direccion[0].lng};
see my example below:
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 100%;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap() {
var direccion=[
{
lugar:'CICESE',
lat:31.8675375,
lng: -116.6686867
},
{
lugar: 'Casa',
lat:31.8915163,
lng:-116.6879557
}
];
var uluru = {lat: direccion[0].lat, lng: direccion[0].lng};//{lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDgMyVeh8DwebGfZHjiNjyle4xFe9pKSdc&callback=initMap">
</script>
</body>
</html>
I tried this following codes:
It only appears like this
What i want to appear
<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 500px;
height: 400px;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
center: {
lat: 44.540,
lng: -78.546
},
zoom: 16
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>
</body>
</html>
You need to add a marker to your map, here is the documentation for adding a marker in javascript: https://developers.google.com/maps/documentation/javascript/markers#add
It works like this:
function initMap() {
var myLatLng = {lat: -25.363, lng: 131.044}; //MARKER POSITION
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!'
});
}
I have included google map in my html(web page), but its not showing the location with red icon(image attached). What code am I missing here?
<!DOCTYPE html>
<html>
<head>
<style>
#map {
width: 800px;
height: 500px;
background-color: #CCC;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
center: {lat: 19.1738076 , lng: 72.8650819 },
zoom: 14
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>
</body>
</html>
You did not define a position/marker, only the center of the map. To set a marker you need to add it to your map.
This is from the google maps api documentation on how to add a a marker:
https://developers.google.com/maps/documentation/javascript/examples/marker-simple
// Define position for your marker
var myLatLng = {lat: -25.363, lng: 131.044};
// Add marker to your map
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Text for your marker!'
});
I've been trying for ages to get a marker on this map, same location as center, but can't get it to work.
<html>
<head>
<style>
#map {
width: 100%x;
height: 400px;
}
</style>
<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(59.909968, 10.748012),
zoom: 16,
zoomControl: true,
scaleControl: true,
scrollwheel: false,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map"></div>
</body>
I've tried following the instructions here, but I can't get it to work..
Any ideas? I had it working on my site using an iframe, but had issues scrolling past the full width map without getting caught zooming in, so I'm looking for a way to make this code to replace it.
Following the example you link to works for me (setting the marker to the map center):
function initialize() {
var mapCanvas = document.getElementById('map');
var mapOptions = {
center: new google.maps.LatLng(59.909968, 10.748012),
zoom: 16,
zoomControl: true,
scaleControl: true,
scrollwheel: false,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
// from https://developers.google.com/maps/documentation/javascript/examples/marker-simple
var marker = new google.maps.Marker({
// change the position to the map center
position: map.getCenter(),
map: map,
title: 'Hello World!'
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
Here is my code for a google map
<!DOCTYPE html>
<html>
<head>
<style>
#map_canvas {
width: 900px;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
function initialize() {
var map_canvas = document.getElementById('map_canvas');
var map_options = {
center: new google.maps.LatLng(18.979026,16.468506),
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var icons = {
parking: {
name: 'Parking',
icon: iconBase + 'parking_lot_maps.png'
}
};
function addMarker(feature) {
var marker = new google.maps.Marker({
position: feature.position,
icon: icons[feature.type].icon,
map: map
});
}
var features = [
{
position: new google.maps.LatLng(-33.91721, 151.22630),
type: 'Parking'
}
var map = new google.maps.Map(map_canvas, map_options)
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
Why is it wrong?
All what it is supposed to do is be centered and have a marker with the icon parking at a certain lat & long.
I tried following the google tutorial, but I understand it
So any help would be much appreciated
Use MarkerImage to place an custom icon
img = new google.maps.MarkerImage('parking_lot_maps.png');
then
Marker = new google.maps.Marker({
position: pos,
icon: img,
});