Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  137] [ 4]  / answers: 1 / hits: 6740  / 10 Years ago, mon, june 16, 2014, 12:00:00

I am trying to change the color of a marker/icon with GoogleMaps API V3 based on user input. I can hard code the color or image link I want into the Javascript, but if I try to pass the color or link as a string variable from my Java program then it either does not show up or defaults to a red marker.



I've looked at all the following threads and at the google API:





All of which have either the link to the marker icon they want to use or the color HEX code already hard coded into the javascript.



My attempt, where address1 and color1 are both strings that are passed to the javascript from my Java code. color1's string, for example could be



http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld•|4D8080:



function codeAddress(address1, color1){
var pinColor = color1;
address=address1;
document.geocoder.geocode( {'address': address1}, function(results, status) {
app.callFromJavascript(address1);
if (status == google.maps.GeocoderStatus.OK) {
document.map.setCenter(results[0].geometry.location);
document.map.setZoom(13);
var marker = new google.maps.Marker({
icon: pinColor,
position: results[0].geometry.location,
map: document.map
});
} else {
alert(Geocode was not successful for the following reason: + status);
}
});
}


This results in the map centering on the location and showing the default red marker.
If I change the string passed to color1 to be http://maps.google.com/mapfiles/ms/icons/yellow-dot.png then no marker shows up at all.



What am I missing that I can't get these to generate from passed variables? Is it simply not possible and mandatory that the location markers be hard coded?


More From » google-maps

 Answers
18

this works fine for me :



var marker = new google.maps.Marker({
icon: new google.maps.MarkerImage(http://maps.google.com/mapfiles/ms/icons/yellow-dot.png),
position: results[0].geometry.location,
map: document.map
});


they do not have to be hard coded , but you do have to use the object type of MarkerImage to pass the url to.



You can not just pass a hex color and expect the maps API to make a new marker based on the color you just picked. What is usually done is to like to an image url that already exists for each color that you plan to use. Then use the code the way I showed you.


[#44527] Saturday, June 14, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
helenat

Total Points: 450
Total Questions: 95
Total Answers: 97

Location: Central African Republic
Member since Mon, Aug 10, 2020
4 Years ago
helenat questions
;