Changing the Google Maps marker/infowindow position - google-maps

I'm currently using this snippet to display my map on a certain page
<div id="map_canvas" style="width: 400px; height: 300px;"></div>
<div class="map_js">
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var myLatlng = new google.maps.LatLng(0, 0);
var mapOptions = {
zoom: 15,
center: myLatlng,
scrollwheel: true,
scaleControl: false,
disableDefaultUI: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var geocoder_map = new google.maps.Geocoder();
var address = 'New York';
geocoder_map.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: map.getCenter()
});
var contentString = 'Hello World New York is the most populous city in the United States.';
var infowindow = new google.maps.InfoWindow({
content: contentString,
maxWidth: 200
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker)
});
infowindow.open(map, marker)
} else {
alert('Geocode was not successful for the following reason: ' + status)
}
});
</script>
</div>
but, as you can see in this image http://d.pr/i/whoI the infowindow is somehow overlapping a bit with the default maps UI.
Does someone know how to solve this issue perhaps?
Best Regards, Jen

Related

Google Map API3 return no map?

Im trying to make marker on google map, based on adress, but maps is showing no data?
Here is my example
$(document).ready(function() {
/* google maps */
google.maps.visualRefresh = true;
var geocoder = new google.maps.Geocoder();
var mapOptions = {
zoom: 14, // set the zoom level manually
zoomControl: false,
scaleControl: false,
scrollwheel: false,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map;
function initialize() {
var address = $('#map').text(); /* change the map-input to your address */
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
if (geocoder) {
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var infowindow = new google.maps.InfoWindow({
content: address,
map: map,
position: results[0].geometry.location,
});
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: address
});
} else {
alert("No results found");
}
}
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
});
HTML
<p id="map">
Vojvode Dobrnjca, Belgrade
</p>
<div id="map-canvas"></div>
You had missed center in mapOptions.
And you should create geocoder variable in initialize function.
Please refer this. I had shown your map.

Google Map direction

I have a google map on a website where I show the position of a business with a custom marker, also, I show the routes to get to the place but I can't seem to understand how to use a custom icon for the START position...
Here is the code I use:
<script>
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer({draggable: true,suppressMarkers: true});
var latlng = new google.maps.LatLng(45.86140239,-74.062538);
var myOptions = {
zoom:13,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: latlng,
mapTypeControl: false,
panControl: false,
streetViewControl: false,
zoomControl: false,
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Marqueur sur le bureau
var icon = {url: 'marker001c.png'};
var headquartersMarker = new google.maps.Marker({
position: latlng,
map: map,
animation: google.maps.Animation.DROP,
icon: icon,
zIndex:99999
});
directionsDisplay.setMap(map);
}
// Calcule de la route
function calcRoute() {
var start = document.getElementById("start").value;
var end = new google.maps.LatLng(45.86140239,-74.062538);
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
directionsDisplay.setPanel(document.getElementById('panel'));
});
var starticon = new google.maps.MarkerImage('test.png');
var marker = new google.maps.Marker({
position:start,
map: map,
icon: starticon,
});
makeMarker({
position: new google.maps.LatLng(60.17295,24.93981),
content: "Some text in the info bubble.",
title: "Tooltip text"
});
}
</script>
By using this code it show me the end icon (my main marker) but I can't change the start icon. Any idea?
start position as string could be used for DirectionsService but for marker you have to change position to LatLng.
See Geocoding Service
For geocoding add new global variable:
...
var map;
var geocoder;
And initialize it:
function initialize() {
geocoder = new google.maps.Geocoder();
directionsDisplay = new google.maps.DirectionsRenderer({draggable: true, suppressMarkers: true});
...
And get the latlng of the start 'address' after directionsService.route():
geocoder.geocode( { 'address': start}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var starticon = new google.maps.MarkerImage('images/start.png');
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
icon: starticon
});
} else {
alert("Geocode failed with status: " + status);
}
});
See example at jsBin. Note: it won't work there because I use my icon images so put in paths to your icon images.

multiple geocoder requests google maps api v3

Hi I've been using Google Maps API v3 and geocoder to display a map and it's working well so far. When I tried to display another map on the same page only the first map loads. The second map simply shows a gray area. No map is loaded. I have to do multiple geocoder requests, but how do I go about it. Here is my code so far:
var geocoder;
var map;
var map2;
var marker;
var infobox;
function initialize() {
//Geocoder
geocoder = new google.maps.Geocoder();
var address = 'Times Square, New York';
geocoder.geocode( { 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
//Marker
var companyLogo = new google.maps.MarkerImage('images/pin.png',
new google.maps.Size(20,20),
new google.maps.Point(0,0),
new google.maps.Point(10,10)
);
//Marker shadow
var companyShadow = new google.maps.MarkerImage('images/pin_shadow.png',
new google.maps.Size(25,33),
new google.maps.Point(0,0),
new google.maps.Point(0, 25)
);
//Display the marker
var marker = new google.maps.Marker({
map: map,
icon: companyLogo,
shadow: companyShadow,
position: results[0].geometry.location,
title:"Some title"
});
//Infobox
infobox = new InfoBox({
content: document.getElementById("infobox"),
disableAutoPan: false,
maxWidth: 320,
pixelOffset: new google.maps.Size(-142, -150),
zIndex: null,
boxStyle: {
opacity: 1,
width: "300px"
},
closeBoxMargin: "0px",
closeBoxURL: "images/close_tooltip.png",
infoBoxClearance: new google.maps.Size(1, 1)
});
//Open infobox
google.maps.event.addListener(marker, 'mouseover', function() {
infobox.open(map, this);
map.panTo(geocoder);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
//Map options
var myOptions = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
scrollwheel: false
}
//Call Maps
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
map2 = new google.maps.Map(document.getElementById("map_canvas2"), myOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
The myOptions object you use to set up the second map does not contain a center property, so the second map cannot be centred.
When the geocoder is called, you set the centre of the first map:
map.setCenter(results[0].geometry.location);
but you do not set the second map.
Without setting the map's centre, it will remain grey and empty.

Adding multiple markers in Google Maps V3 using Json

I'm trying to adapt my code with json in google maps. I tried looking at other threads of stackoverflow but did not help me.
How do I show multiple points on the map with the coordinates that are received by json?
This code in firebug shows no errors and the map appears all blue.
var geocoder;
var map;
function initialize() {
var url = '/project/display/get_coordinates/';
$.getJSON(url, function(data) {
$.each(data, function(index, c) {
//alert(c.fields['lat']+','+c.fields['lng']); result: -23.0522826,-43.32745712
//Map
latlng = new google.maps.LatLng(c.fields['lat']+','+c.fields['lng']);
//options
var myOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: true
};
//get the map
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//marker
var marker = new google.maps.Marker({
map: map,
position: latlng,
//title: {{i.display.codigo}}
});
//infowindow
var infowindow = new google.maps.InfoWindow({
content: 'oi'
});
//click infowindow
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
});//close each
});//close getjson
}//close initialize
My response in Json:
[{"pk": 1, "model": "address.coordinates", "fields": {"lat": "-23.0522826", "lng": "-43.32745712"}}, {"pk": 2, "model": "address.coordinates", "fields": {"lat": "-22.24569326", "lng": "-43.7028948"}}]
Thanks!
You have 1 real error here:
latlng = new google.maps.LatLng(c.fields['lat']+','+c.fields['lng']);
google.maps.LatLng expects 2 parameters(lat &lng) , but you provide 1(a string containing lat and lng, joined by a comma) . It has to be
latlng = new google.maps.LatLng(c.fields['lat'],c.fields['lng']);
But you also have a logical error:
You overwrite the last map created on every each-loop
Create the map and the infoWindow outside of the $.each()
function initialize()
{
var myOptions =
{
zoom: 5,
center: new google.maps.LatLng(1,1) ,
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: true
};
var url = '/project/display/get_coordinates/';
$.getJSON(url, function(data) {
map= new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var bounds=new google.maps.LatLngBounds();
infowindow = new google.maps.InfoWindow({
content: 'oi'
});
$.each(data, function(index, c) {
var latlng = new google.maps.LatLng(c.fields['lat'],c.fields['lng']);
var marker = new google.maps.Marker({
map: map,
position: latlng,
title:'pk:'+c.pk
});
bounds.extend(latlng);
google.maps.event.addListener(marker, 'click', function() {
infowindow.close();
infowindow.setContent('pk:'+c.pk);
infowindow.open(map, marker);
});
});//close each
map.fitBounds(bounds);
});//close getjson
}//close initialize

Small Google Maps Infowindow - Smaller than 200px

I have a map_canvas with a width of 225.
I want my infobox to be smaller than this (eg. 150 px).
Is this possible? I tried maxWidth and adding styles, but nothing seemed to work.
A screen (on imgshack) can be found below:
Also,
I'm using v3 of Google Maps
Javascript:
var results_coods;
var map_2;
var marker_2;
var infowindow_2;
function call_gmap()
{
if($('#map_text').attr('lat')){
var lat = Number($('#map_text').attr('lat').replace(",","."));
var lng = Number($('#map_text').attr('long').replace(",","."));
initialize(lat,lng);
/*
var lat= '51.0153389';
var lng = '2.7253832';
*/
}
}
function initialize(lat,lng) {
var latlng = new google.maps.LatLng(lat,lng);
var myOptions = {
zoom: 13,
center: latlng,
disableDefaultUI: true,
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
draggable: false,
scaleControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var infowindow = new google.maps.InfoWindow({
content: $('#map_text').html(),
maxWidth: 150
});
var image = 'beachflag.png';
var marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.open(map,marker);
google.maps.event.addListener(
marker, 'click', function() {
infowindow.open(map,marker);
});
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );
}
function GcodeAddress()
{
var map;
geocoder = new google.maps.Geocoder();
var address = $('#map_address').html();
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var myOptions = {
zoom: 13,
center: results[0].geometry.location,
navigationControl: false,
scaleControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infowindow = new google.maps.InfoWindow({
content: $('#map_text').html()
});
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
results_coods = results[0].geometry.location;
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
infowindow.open(map,marker);
google.maps.event.addListener(
marker, 'click', function() {
infowindow.open(map,marker);
});
google.maps.event.trigger(map, 'resize');
marker_2 = marker;
map_2 = map;
infowindow_2 = infowindow;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
function resizeMap()
{
//hiervan komt 1 error, de andere van infowindow
google.maps.event.trigger(map_2, 'resize');
map_2.setCenter(results_coods);
infowindow_2.open(map_2,marker_2);
}
Javascript, bottom of html page:
$(document).ready(function($) {
GcodeAddress();
resizeMap();
});
HTML (VB.Net):
'-- Google Map
g_map_text.Text = "<div id=""map_canvas"" style=""width: 225px; height: 225px; position: relative;"" /></div>" & vbCrLf
g_map_text.Text &= String.Format("<div id='map_text' style=""display:none;width:150px;"">{0}</div>", LoadKantoor)
g_map_text.Text &= String.Format("<div id='map_address' style='display:none;' >{0}</div>", Bedrijf.Bedrijf("postalcode") & " " & Bedrijf.Bedrijf("city") & "," & Bedrijf.Bedrijf("address"))
An easier approach is to style using CSS. You need wrap the contents of the infoWindow in a div with your custom CSS.
Javascript:
marker.openInfoWindowHtml('<div class="iwContainer">' + yourContent + '</div>');
CSS:
.iwContainer {
width: 300px;
height: 200px;
}
Are you setting the maxWidth for the infowindow before the call to open as described here
EDIT:
I've looked at this again and it looks fine. I don't understand why it doesn't work.
You could use InfoBox.js instead?
API Reference: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/reference.html
Download JS: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/
Examples: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/examples/
Let me know how you get on.
Maybe you interested using this infobubble:
http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/examples/example.html/
you can apply css on this and easy to custom with the example