Markers Google Maps V3 Javascript

Asked

Viewed 516 times

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!

1 answer

2


The initialize function called in the last line of your code does not exist. It must replace the function name carregarMapa() for initialize(). The function criaPonto() is it never called? You need to create a method in the initialize function that detects the proximity of new points and then calls the function criaPonto().

You have to create an array type variable with markers, and then add each marker to that variable, via the function criaPonto() with markers.push(marker2).

  • 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()"/>

  • 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?

  • 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

  • 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.

  • I’ve downloaded this site, but I can’t find that I asked here in stackflow.

  • 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?

  • 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...

  • That file is restricted, you have to give access.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.