1
var map
function detectBrowser() {
var useragent = navigator.userAgent;
var mapdiv = document.getElementById("map_canvas");
if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
mapdiv.style.width = '100%';
mapdiv.style.height = '100%';
} else {
mapdiv.style.width = '600px';
mapdiv.style.height = '800px';
}
}
function carregarMapa() {
lat=document.getElementById("txtLat").value;
lng=document.getElementById("txtLng").value;
var myLatlng = new google.maps.LatLng(lat,lng);
var mapOptions = {
zoom: 16,
center: myLatlng,
disableDefaultUI : false
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
map.setOptions({styles: [
{
featureType: "poi",
stylers: [
{ visibility: "off" }
]
}
]});
}
function criaPonto(){
plat=document.getElementById("txtLat").value;
plng=document.getElementById("txtLng").value;
var myLatlng = new google.maps.LatLng(plat,plng);
var marker2 = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Título Restaurante Y'});
}
google.maps.event.addDomListener(window, 'load', initialize);
Hello, I created this code in javascript trying to create a google map where I can load markers, until now, I could only create markers if I keep resetting the map, and the idea was to create these markers at runtime. For example: I would create a Marker if it was X away from the point my sensor has located, otherwise this Marker is not displayed.
The problem has been solved! I appreciate the user’s help : /users/10196/migmar Who was very present and was able to explain to me my mistakes.
Thank you!
Hello, thanks for the answer... So the function loadMap() and createPoint() are called by buttons in my html form, follow the code: <body onload ="loadMap()"> input id="createPont" type="button" value="Create Point" onclick="createPoint()"/>
– Tiaurls
Okay, I get it. Let’s go in parts, the function loadMap() can be executed this way. But the last line of code "google.maps.Event.addDomListener(window, 'load', initialize);" exists for that very reason. Look, first parameter is "window" + second parameter "load". This equals "body onload". The third parameter is the name of the function that should be called when the page finishes loading. By default this function is called initialize, Voce has chosen to load Pad. Remove onload from the <body> element and replace initialize by loading Pad. Now, as the value of txtLat is updated?
– MigMar
Okay, I changed the line and deleted the onload from the body. Thanks for the tip! I understood why it’s called initialize thanks to your explanation! The value of txtLat and txtLng is updated by a field : <input id="txtLat"> for this reason gives a Document.getElementById in the code. I had tried to leave the map as a global variable, but I could not load it into the functions. I will try again later
– Tiaurls
I have some articles for beginner in Google Maps API published in http://www.marnoto.com. Perhaps you can help understand the structure of creating maps and markers.
– MigMar
I’ve downloaded this site, but I can’t find that I asked here in stackflow.
– Tiaurls
I still don’t quite understand what you want to do. You can indicate the link to your site so I can take a look at the code?
– MigMar
It’s not online, it’s a local project, but I’ll link you to the project so you can test locally. https://drive.google.com/file/d/0B5kTRRclWirSamNKc183V0FGNHM/edit?usp=sharing What I want is to include markers without reloading the map, at the moment I can only include a marker and for that I need to reload the map (even using it as a global variable) In fact I will pass this to a mobile APP, and I’m still in the learning and testing phase, but in the future I will need access to the user’s GPS as well...
– Tiaurls
That file is restricted, you have to give access.
– MigMar