Displaying multiple locations on Maps (iframe) - google-maps

Is there a way to achieve something exactly like this?
I need to add two separate locations on a single map. Two divs on the top-left are important.
I created a map and added two locations. But the view we get is completely different from the normal google maps.

You can use Google Maps API to do that as shown here (I've just apply some small modifications to original answer to make it pure JavaScript-based):
var mapDiv = document.createElement('div');
mapDiv.setAttribute("id", "map");
mapDiv.style.cssText = 'display: table; width: 100vw; height: 100vh;';
document.body.appendChild(mapDiv);
var locations = [
['<h3>Dnipro</h3>You must visit this city!', 48.47672904338519, 34.99977516526972, 4],
['<h3>Lvyv</h3>A western capital of Ukraine', 49.84196798934066, 24.0256235253287, 3],
['<h3>Kharkiv</h3>A first capital of Ukraine', 49.9968725638038, 36.232661130479805, 2],
['<h3>Kyiv</h3>A capital of Ukraine', 50.464300,30.494800, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: new google.maps.LatLng(50.4,30.4),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
You can see the the result on Codepen
Info divs can be added in that way (just a sample):
<style>
#over_map { position: absolute; top: 10px; left: 10px; z-index: 99;
width:200px;height:100px;border:1px solid black; background: white; }
</style>
<div id="over_map">
<h4>This is a first place</h4>
</div>

Related

create a polyline with multiple points, then join an added marker to a line segment google maps v3

I have a google map that I am plotting a route on and linking my points with a polyline. I create a new point on the line with a right click on the map. I also have the ability to create a point of interest along my path and I'm using a different marker icon for those as well as showing an info window with the POI text.
google.maps.event.addListener(Map, "rightclick", function (event) {
var markerIndex = polyLine.getPath().length;
polyLine.setMap(Map);
var isFirstMarker = markerIndex === 0;
var marker;
var poiText = "";
if(!isAddingPOI) {
marker = new google.maps.Marker({
map: Map,
position: event.latLng,
draggable: true,
icon: '/images/waypoint.png'
});
} else {
poiText = prompt("Enter a short description for this point of interest", "");
if (poiText != null) {
marker = new google.maps.Marker({
map: Map,
position: event.latLng,
draggable: true,
icon: '/images/point_of_interest.png',
info: new google.maps.InfoWindow({
content:poiText
})
});
}
}
if (isAddingPOI) {
predefinedPositions.push(new predefinedPosition(event.latLng.lat(), event.latLng.lng(), poiText));
google.maps.event.addListener(marker, 'click', function () {
marker.info.open(Map, marker);
});
isAddingPOI = false;
} else {
predefinedPositions.push(new predefinedPosition(event.latLng.lat(), event.latLng.lng()));
}
polyLine.getPath().push(event.latLng);
outlineMarkers.push(marker);
google.maps.event.addListener(marker, 'drag', function (dragEvent) {
polyLine.getPath().setAt(markerIndex, dragEvent.latLng);
predefinedPositions[markerIndex].Latitude = dragEvent.latLng.lat();
predefinedPositions[markerIndex].Longitude = dragEvent.latLng.lng();
updateDistance(outlineMarkers);
});
updateDistance(outlineMarkers);
});
what I need now is the ability for the user to specify a LatLng for a new marker which would be placed on the map as draggable and then to be able to merge (by dragging) that marker to my created path. Ideally, I'd like for the segment of the polyline where the marker is dragged to change colors while the marker is over it. I'll then need to add that marker to the path array for the final output.
I found this which is the closest to what I want that I could find but it's not quite what I'm looking for: Google Maps API v3 - Draggable Marker Along a Polyline
I also tried adding a 'mouseover' event to my polyline to update the color which doesn't ever get triggered while I'm dragging a marker. It also updates the entire polyline's color (not just the segment) when I mouse over it. I'm sure I can sort out the segment color change myself, but I'm stuck on dragging the marker over the line and having it change colors and ultimately merging it to my path.
any suggestions would be greatly appreciated.
TIA
I got it sorted out using something similar to this: Confine dragging of Google Maps V3 Marker to Polyline
I didn't get the polyline segment to change colors, but this "snap-to" functionality is really close to what I was looking for.
try this way
//data sample
var cities = [
{
city : 'Mayestik Tailor',
desc : 'Jln Aspal Rata',
lat : -6.2426373,
lng : 106.7907953
},
{
city : 'Bubur Ayam Barito',
desc : 'Jl. Gandaria Tengah 3, Kramat Pela, Kebayoran Baru',
lat : -6.2457674,
lng : 106.790865
},
{
city : 'Apartment Permata Gandaria',
desc : 'Jl. Kyai Moh. Syafii Hadzami/Jl. Taman Gandaria',
lat : -6.2450461,
lng : 106.7882604
}
];
create function to center point of layout
var mapOptions = {
zoom: 15,
center:new google.maps.LatLng(-6.2451528,106.7901808),
mapTypeId: google.maps.MapTypeId.TERRAIN
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var createMarker = function (info){
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(info.lat, info.lng),
title: info.city
});
}
var arr = [];
for (i = 0; i < cities.length; i++){
arr.push(new google.maps.LatLng(cities[i].lat, cities[i].lng));
}
// create polyline
var flightPath = new google.maps.Polyline({
path: arr,
geodesic: true,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
// loop for marker
for (i = 0; i < cities.length; i++){
createMarker(cities[i]);
}
for css class
#map {
height:420px;
width:600px;
}
.infoWindowContent {
font-size: 14px !important;
border-top: 1px solid #ccc;
padding-top: 10px;
}
h2 {
margin-bottom:0;
margin-top: 0;
}
on HTML Code
<div id="map"></div>

Google Maps API V3 (JS): Multiple directions on one map

I know this question has been answered before, but unfortunately, all links to demos/explanation pages are dead at the moment.
I would like to use the Google Maps API V3 to plot two directions (one for driving and one for walking) on the same map. If possible, I would like to use Google's default 'directions' lines, since these look nicer than the polylines.
Does anybody know how to do this? Any help is greatly appreciated!
Here is an example that I have made in the past for this. The trick is multiple DirectionsRenderer instances.
var MAP,
DIRECTIONSRENDERER1,
DIRECTIONSRENDERER2,
USER,
PLACE1,
PLACE2;
function displayDirections1(result, status) {
DIRECTIONSRENDERER1.setDirections(result)
}
function displayDirections2(result, status) {
DIRECTIONSRENDERER2.setDirections(result)
}
function getDirections(location, displayCallback) {
//https://developers.google.com/maps/documentation/javascript/3.exp/reference#DirectionsRequest
var request = {
travelMode: google.maps.TravelMode.DRIVING,
origin: USER,
destination: location
};
//https://developers.google.com/maps/documentation/javascript/3.exp/reference#DirectionsService
var service = new google.maps.DirectionsService();
service.route(request, displayCallback);
}
//Set up the map
function initialize() {
var mapOptions = {
zoom: 6,
//I used the center of the USA
center: new google.maps.LatLng(38.8282, -98.5795)
};
//Make the map
MAP = new google.maps.Map(document.getElementById('map'), mapOptions);
DIRECTIONSRENDERER1 = new google.maps.DirectionsRenderer({
map: MAP
})
DIRECTIONSRENDERER2 = new google.maps.DirectionsRenderer({
map: MAP
})
//Make marker for User location
//NOTE: for testing, the user position is fixed
USER = new google.maps.LatLng(38.8282, -98.5795);
new google.maps.Marker({
label: 'U',
cursor: "User Location",
position: USER,
map: MAP
});
//used for this demo
document.getElementById("latitude").textContent = USER.lat();
document.getElementById("longitude").textContent = USER.lng();
//Make marker for Place1 (location is arbitrary)
PLACE1 = new google.maps.LatLng(37, -97);
new google.maps.Marker({
label: '1',
cursor: "Place 1",
position: PLACE1,
map: MAP
});
//Make marker for Place2 (location is arbitrary)
PLACE2 = new google.maps.LatLng(39, -102);
new google.maps.Marker({
label: '2',
cursor: "Place 2",
position: PLACE2,
map: MAP
});
document.getElementById("p1button").addEventListener("click", function(e) {
getDirections(PLACE1, displayDirections1);
});
document.getElementById("p2button").addEventListener("click", function(e) {
getDirections(PLACE2, displayDirections2);
});
//Trigger map redraw when dom element is resized
google.maps.event.addDomListener(window, 'resize', function() {
google.maps.event.trigger(MAP, 'resize');
});
//Preserve map perspective when after resize
google.maps.event.addListener(MAP, 'resize', function() {
var center = MAP.getCenter();
google.maps.event.addListenerOnce(MAP, 'center_changed', function() {
MAP.setCenter(center);
});
});
}
//runs the code to set up demo
initialize();
html,
body,
#map {
height: 100%;
width: 100%;
border-width: 0;
margin: 0;
padding: 0;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<b>Your location:</b>
<br/>
<span>Latitude: </span><span id="latitude"></span>
<br/>
<span>Longitude: </span><span id="longitude"></span>
<br/>
<br/>
<button id="p1button">Directions to Place1</button>
<button id="p2button">Directions to Place2</button>
<div id="map"></div>

Add a label with the special char +

I am drawing custom markers on a map and adding labels based on how many people are at that particular address. If there are more than 5 I want to write the label 5+ instead of writing out 23 for example. 1 - 4 work just fine, but I cannot seem to pass the + in a manner that it will display. I do not want to use a static image/marker with the number if I can help it. Anyone done this before?
I have tried this:
var res_label = '5+'
var marker = new google.maps.Marker({
position: latLng,
map: map,
icon: img,
label: encodeURIComponent(res_label),
animation: google.maps.Animation.DROP
});
The pin drops but is blank. I tried wrapping the marker var and that didn't work either.
In a URL a + will be used as space, you must encode it: %2B
You should use encodeURIComponent() to encode the entire string(there may be more chars which must be encoded)
window.onload = function() {
document.body.firstChild.data = encodeURIComponent(document.body.firstChild.data);}
^1234567890ß´`?=)(/&%$§"!+*~ÄÖLK_:;-.,Q>%
sample-implementation:
function initialize() {
var goo = google.maps,
map = new goo.Map(document.getElementById('map_canvas'), {
zoom: 14,
center: {
lat: 0,
lng: 0
}
}),
//note:the dynamic chart-icons are deprecated and may be disabled som day
url = 'https://chart.googleapis.com/chart?' +
'chst=d_bubble_text_small&chld=',
marker = new goo
.Marker({
map: map,
position: map.getCenter(),
icon: url +
encodeURIComponent('bb|text|FFFFFF|000000')
}),
btn = document.createElement('input');
btn.setAttribute('type', 'button');
btn.setAttribute('value', 'push me!');
map.controls[goo.ControlPosition.TOP_CENTER].push(btn);
goo.event.addDomListener(btn, 'click', function() {
var txt;
if (txt = prompt('type a string', 'text')) {
marker
.setIcon(url + encodeURIComponent('bb|' + txt + '|FFFFFF|000000'))
}
})
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
height: 100%;
margin: 0;
padding: 0
}
input {
background: tomato;
font-size: 22px;
border-color: tomato;
border-radius: 8px;
padding: 4px;
}
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script>
<div id="map_canvas"></div>

Customizing google map with multiple markers and info window

*Hi there, i am new to the whole Google map api environment so please do guide me along.
What i am trying to achieve below is to extract data from either a XML file or a JSON file and plot the locations onto Google map. I am also trying to add a info window whereby it will show different information for each location. I understand that to have a info window, i would have to create a over lay. But the question is how do i actually tag multiple info-window to their markers ?
All markers are displayed well on the map, but the problem is when I click to see info window - info window is always displayed on the same marker. What's the problem?
This is what i have came up with so far and i would greatly appreciate if anyone is able to spot the issue.
<!DOCTYPE html>
<html>
<head>
<style>
html, body, #map_canvas { margin: 0; padding: 0; height: 100%; }
</style>
<script
src="https://maps.googleapis.com/maps/api/js sensor=false&libraries=visualization">
</script>
<script>
var map;
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(-27.48939, 153.012772),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var script = document.createElement('script');
script.src = 'http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week';
document.getElementsByTagName('head')[0].appendChild(script);
}
window.eqfeed_callback = function(results) {
for (var i = 0; i < results.features.length; i++) {
var earthquake = results.features[i];
var coords = earthquake.geometry.coordinates;
var latLng = new google.maps.LatLng(coords[1],coords[0]);
var marker = new google.maps.Marker({
position: latLng,
map: map,
});
var infowindow = new google.maps.InfoWindow({
content: "<div>Hello! World</div>",
maxWidth:100
});
google.maps.event.addListener(marker, "mouseover", function() {
infowindow.open(map, marker);
});
}
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
</html>
Use this instead of marker, when opening infowindow.
google.maps.event.addListener(marker, "mouseover", function() {
infowindow.open(map, this);
});

Causing links to pan to and open markers in map

JS Fiddle:
http://jsfiddle.net/megatimes/NVDLf/7/
I have a map which is creating multiple markers from an array.
Below the map are some links which, when clicked, I'd like to cause the map to pan to its respective marker, and open the info window.
Given that I don't want to generate the links themselves as part of the loop that creates the markers, how can I do this?
I'm fairly certain this is a scope issue since the links below the map each open the last item in the locations array but I can't seem to figure out how to get around it.
Thanks
var locations = [
[
"Location 1",
"215 West Girard Avenue 19123",
"39.9695601",
"-75.1395161"
],
[
"Location 2",
"5360 Old York Road 19141",
"40.034038",
"-75.145223"
],
[
"Location 3",
"1350 W Girard Avenue 19123",
"39.9713524",
"-75.1590360"
]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(39.9995601, -75.1395161),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][2], locations[i][3]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
$(function() {
$('#locations h3 a').each(function() {
$(this).on('click', function() {
google.maps.event.trigger(marker, 'click');
})
});
});
<div id="map" style="width: 500px; height: 400px;"></div>
<div id="locations">
<h3>Location 1</h3>
<p>Arbitrary content about 1</p>
<h3>Location 2</h3>
<p>Arbitrary content about 2</p>
<h3>Location 3</h3>
<p>Arbitrary content about 3</p>
</div>
You can do something like this:
http://jsfiddle.net/6vjwd/2/embedded/result/
uses a createMarker function to associate the infowindow with the marker:
function createMarker(latlng, html)
{
var marker = new google.maps.Marker({
position: latlng,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
});
return marker;
}
And a global array to allow them to be referenced from HTML click listeners.
The external links depend on knowing the order of the locations. Or if you want to look them up by "name" you could use an "associative" array, with the name as the index.
Indexing the markers by name:
http://jsfiddle.net/6nqj8/1/embedded/result/
I would generate the links using jQuery while generating the markers. You can store the marker object on the link object using jQuery's .data() call and add a generic click handler to those links that set the map to the marker's location.
Something kind of like this:
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][2], locations[i][3]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
$('#locations').append($('<a>').append('Location Name').click(function()
{
// $(this).data('marker')
}).data('marker', marker));
}
ok, this works for me. I'm adding each marker to a global variable array called markers. Then whenever a link is clicked, check which link in the array of those links it is by using the jquery .index() function, and trigger the 'click' on the corresponding marker (link 1 = marker 1, etc).
Thanks to geocodezip as well for the createMarker function which I've re-used here.
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map { width: 500px; height: 400px; }
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var marker, map;
var markers = [];
function initialise() {
var i;
var locations = [
[
"Location 1",
"215 West Girard Avenue 19123",
"39.9695601",
"-75.1395161"
],
[
"Location 2",
"5360 Old York Road 19141",
"40.034038",
"-75.145223"
],
[
"Location 3",
"1350 W Girard Avenue 19123",
"39.9713524",
"-75.1590360"
]
];
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: new google.maps.LatLng(39.9995601, -75.1395161),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
infowindow = new google.maps.InfoWindow();
for (i = 0; i < locations.length; i++) {
markers.push(createMarker(new google.maps.LatLng(locations[i][2], locations[i][3]), locations[i][0]));
}
}
function createMarker(latlng, html)
{
var marker = new google.maps.Marker({
position: latlng,
map: map
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
});
return marker;
}
$(function() {
$('#locations h3 a').each(function() {
$(this).on('click', function() {
// find out which link in the array was clicked (1st, 2nd etc)
var intLinkClicked = $('#locations h3 a').index($(this));
google.maps.event.trigger(markers[intLinkClicked], 'click');
});
});
});
google.maps.event.addDomListener(window, 'load', initialise);
</script>
</head>
<body>
<div id="locations">
<h3>Location 1</h3>
<p>Arbitrary content about 1</p>
<h3>Location 2</h3>
<p>Arbitrary content about 2</p>
<h3>Location 3</h3>
<p>Arbitrary content about 3</p>
</div>
<div id="map"></div>
</body>
</html>