I'm introducing myself to Google Maps API, and to JavaScript as well.
I already have set my map on my website, and now I'm trying to dynamically set multiple markers on the same map.
For that, I wrote this function:
function teste(lat,long){
alert (lat);
alert (long);
//var companyPos3 = new google.maps.LatLng(41.545308,-8.421782);
var companyPos3 = new google.maps.LatLng(lat,long);
var companyMarker3 = new google.maps.Marker({
position: companyPos3,
map: map,
// icon: companyLogo,
title:"Mar!!!" });
//}
...
}
And my problem resides right here. I can't set the google.maps.LatLng with 'lat' and 'long' function parameters. However, the alert dialog messages pop up, showing the values that are from PHP.
I tried also to remove the alert function but the marker didn't show as well.
I'm not sure if it there is a Google Maps API trick, or JavaScript trick.
Oh, and if I set the values as the commented line, it works, but I don't want this solution :).
long is the reserved word in JS. Please check here. So, please try lng instead of long also for setting icon please see here. Try something like
function teste(lat,lng){
alert (lat);
alert (lng);
//var companyPos3 = new google.maps.LatLng(41.545308,-8.421782);
var companyPos3 = new google.maps.LatLng(lat,lng);
var companyMarker3 = new google.maps.Marker({
position: companyPos3,
map: map,
// icon: companyLogo,
title:"Mar!!!" });
//}
...
}
Related
I am trying to add a custom marker from an array on Google Maps I have seen another question close to this but the code provided doesn't allow Google Maps API to pick the Marker Icon
function button1(location) {
var get1 = prompt ("Enter First Coord");
var get2 = prompt ("Enter Second Coord");
var icons = [
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_green.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_purple.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_orange.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_white.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_black.png",
"https://raw.githubusercontent.com/Concept211/Google-Maps-Markers/master/images/marker_blue.png"
];
var items;
var center = new google.maps.LatLng(get1,get2);
map.panTo(center);
var marker = new google.maps.Marker({
position: center,
map: map,
icon: items[Math.floor(Math.random()*icons .length)]
});
I keep getting a "items is not defined".
If I define it above the code I get a different error saying
"Cannot read property '3' of undefined" on the icon: items[Math.floor(Math.random()*icons .length)] code
If anyone knows the solution I would very much appreciate it, thanks!
You are trying to get content of empty variable items... Replace
icon: items[Math.floor(Math.random()*icons .length)]
with:
icon: icons[Math.floor(Math.random()*icons .length)]
Because you haven't got saved pictures in variable items but in variable icons.
here is my code in html to generate marker and infowindow(with ruby on rails)
var marker=[]
function initMap() {
var latLng1 = new google.maps.LatLng(1.352083, 103.819836);
var myOptions = {
zoom: 12,
center: latLng1,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
for(i=0;i<gon.astatic.length;i++){
var latLng = new google.maps.LatLng(gon.astatic[i][1], gon.astatic[i][2]);
if(i<2){
marker[i] = new MarkerWithLabel({position: latLng, map: map,icon:"/assets/green_MarkerV.png" ,labelClass: "labels",labelContent: gon.astatic[i][3]});}
else
{
marker[i] = new MarkerWithLabel({position: latLng, map: map,icon:"/assets/green_MarkerN.png" ,labelClass: "labels",labelContent: gon.astatic[i][3]});
}
var iw =new google.maps.InfoWindow({content: 'HI' });
google.maps.event.addListener(marker[i],"mouseover",function(e){iw.open(map,marker[i]);})
}
this gon is just some 'stupid' method I use to pass data from ruby on rails controller to javascript.
for all marker,the infowindow all appear at corner.
But for my another map(which have only one marker with infowindow)it works fine.
What might be my problem?why this infowindow appear in wrong position?Instead of just above the marker?
EDIT:
After half day's trouble shoot,I feel the problem is at
google.maps.event.addListener(marker[i],"mouseover",function(e){iw.open(map,marker[i]);})
when the listener calls back,the value inside marker is i ,which is not a actual number,so the marker display at a corner.I feel the problem is can't pass variable into addListener,can only put in actual number.How to solve this?
Each instance of the function declared inside the for loop shares the same closure containing the value i, and so all of your addListener calls are essentially calling iw.open(map, undefined) since i will be off the end of the array at the end of the iteration.
See JavaScript closure inside loops – simple practical example for sample solutions to this problem, and How do JavaScript closures work for more information about closures in JavaScript in general.
The problem is with your MarkerWithLabel library. Infowindow take position from marker. Try use this link http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.8/docs/examples.html . It has all the things that you want to implement. It it's not work then you can also set position for infowindow with setPosition() function just pass latlng that you used to create marker and you are done.
i dont recommend using new gem just to pass data from ruby to js...you can do this simply by many ways...your code seems good but i cannot say how gon is handling your js script.Please take a look at this similar question where i have implemented the same dynamic map with dynamic markers and infowindows.This code is working great
see here
I have a Google Maps marker function that successfully creates markers on a map like this:
// A function to create the marker and set up the event window
function createMarker(point,html) {
var marker = new GMarker(point,{title:html});
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
Here is a tinyurl of the exiting code: http://tinyurl.com/b8f9b4l
Using this solution: Google maps: place number in marker?
I've updated this line of code but it is not numbering. What am I doing wrong?
var marker = new GMarker(point,{title:html,icon:'icon: \'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld='+ (position) +'|FF776B|000000',});
The icon property just needs to be the url. You don't need the extra "icon:", and you should drop the extra comma at the end (IE seems to throw an exception when it finds a dangling comma). Also, the parenthesis you don't need - but probably aren't hurting anything.
{
title:html,
icon: 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=' + position +'|FF776B|000000'
}
I see where you got the idea. Idk why s/he got a point for that. The extra "icon:" messes it up.
Try this as a test, it should make sure you don't have any problems with the variables inside the url.
{
title:html,
icon: 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=4|FF776B|000000'
}
I am using gmap v3. I am facing a problem.
The problem is that I am creating dynamically marker and at similar time I am creating the infowindow of that marker.
Now I want to add some contents in any infowindow of a marker.
But don't know how i can get the content of a infowindow.
I have stored my markers objects in a array and also infowindow's objects.
But not found any solution.
I want to get infowindow's content on the basis of marker.
EDIT:
var markerArray = new Array();
var infoArray = new Array();
function placemarker(point,id, contents){
var marker = new google.maps.Marker({
icon: image,
position: point,
map: map,
title: name
});
markerArray[id] = marker;
var infoBubble = new InfoBubble();
var content = contents;
infoBubble.setContent(content);
google.maps.event.addListener(marker,"mouseover",function(event){
for(var i=0; i < infoArray.length ; i++ )
infoArray[i].close();
infoBubble.open(map,marker);
});
infoArray.push(infoBubble);
}
This function is called within a function many time that create marker on map.
now on a condition two markers are at same lat long and I want to show single marker with infowindow of both markers content. I have able to create single marker but not able to append the content in a info window.
If you are already keeping all the InfoWindow's in your infoArray, why don't you simply store them with the same id as you do with your markers?
var markers = {};
var infoWindows = {};
function placemarker(point, id, contents) {
var marker = ...
...
markers[id] = marker;
var infoWindow = ...
...
infoWindows[id] = infoWindow;
}
(Note that I have replaced your arrays with hashes.)
Now you can access every marker's InfoWindow (and its content via getContent()) the same way you access the marker itself.
This is pretty old but figured I would give this a shot
I'm not sure what InfoBubble is for my example I'm using InfoWindow.
function createMarker(objPoint)
{
objPoint = new google.maps.LatLng(strLat, strLng);
strFromHtml = 'Your HTML';
//creates the window with the options
objInfoWindow = new google.maps.InfoWindow(
{
content: strToHtml,
maxWidth: 250,
zIndex: -1
});
var markerOptions =
{
position: objPoint,
map: objMap
}
objMarker = new google.maps.Marker(markerOptions);
//if the marker is clicked open the window
google.maps.event.addListener(objMarker, "click", function()
{
objInfoWindow.open(objMap, objMarker);
});
}
I first create the window, then the marker, I then attach a click listener for the marker to display the infowindow on it when the marker is clicked. You could take the click out and just display it but for my purposes I wanted them to have to click it.
EDIT
Ok I've reread your question about 50 times and I hope this solution is good. As for the multiple markers in one location I don't have an answer for that. I think I have an answer for the multiple markers with its own info. If this doesn't work then you are gonna have to create an example in jsfiddle or something otherwise I don't understand what you want. Check it out here jsfiddle
When creating the infoWindow you only create it once holding default content. So put it in the initialization of the map itself. Then when creating the markers you can use the marker itself to contain the html for the infowindow to show.
var markerOptions =
{
position: objPoint,
map: objMap,
html: strHtml//added to make the marker object store the content of the infowindow
}
Then in your mouseover listener you use this.html to get the content for the infowindow
google.maps.event.addListener(objMarker, "mouseover", function()
{
objInfoWindow.setContent(this.html);
objInfoWindow.open(objMap, this);
});
As for the multiple markers in one spot. I would have to say programmatically you are gonna have to catch this and only create one marker with the content of both in one marker.
I don't think it is possible to search for a marker in a google maps (which I think is what you are trying to do).
Do you need to create two markers when they anyway are on the same point? If no I would just keep track of the infoBubbles in an array where the point coordinates is the key. So before you create a new infoBubble you check if you already have one and if that is the case you just replace it with a new combined infoBubble.
Here is a code draft.
if(infoArray[point.lat+'_'+point.lng] != undefined){
tmpBubble = infoArray[point.lat+'_'+point.lng];
infoBubble = createCombinedBubble(tmpBubble, infoBubble);
}
infoArray[point.lat+'_'+point.lng] = infoBubble;
Hello I'm new to rails in general.
I'm building a web application using rails in which I need to render different people locations using google map.
Basically I want to generate different color markers depending on user group. Now how do I do that processing in the javascript file?
You can use MapIconMaker Library for coloring markers, you can define many colors to manege your groups:
var GROUP_1 = MapIconMaker.createMarkerIcon({primaryColor: "#04b404"});
var GROUP_2 = MapIconMaker.createMarkerIcon({primaryColor: "#58acf4"});
var GROUP_3 = MapIconMaker.createMarkerIcon({primaryColor: "#ff8000"});
point = new GLatLng(latitude, longitude);
marker = new GMarker(point,{icon: GROUP_1});
Hope this helps
If I remember correctly, other than the default icon, you must use an image for the icon in other coloring scenarios as described here : http://code.google.com/apis/maps/documentation/javascript/reference.html#MarkerOptions
In an implementation I've used before, it looked similar to this:
$.each(data, function() {
var post_position = new google.maps.LatLng(this.latitude, this.longitude);
var marker = new google.maps.Marker({
position: post_position,
map: map,
title: this.title,
icon: "images/purple_icon.png"
});
markerArray.push(marker);
})
You can use gmaps4rails and customize your markers according to whatever rule you want.
See screencasts (I know res is poor but vote on Railscasts!).