Markers Google Maps V3 JavaScript

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, so far, I only managed to create markers if I reset 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 located, otherwise that marker is not displayed.

The issue has been resolved! Thanks for the help from the user: link That was very present and managed to explain my mistakes.

Thanks!

    
asked by anonymous 02.06.2014 / 17:53

1 answer

2

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

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

    
03.06.2014 / 08:34