Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

xabud

Markers - Icones Personalizados Google Maps

Recommended Posts

Olá! Gostaria de implementar no array var myLatlng icones individuais e fixos para cada um deles, ao inves de utilizar a função abaixo que coloca o mesmo icone para todos. Sou leigo em js, acredito que alguem possa me ajudar. O codigo abaixo é basicamente o mesmo que no site de desenvolvedor do google maps, segue o link original: https://developers.google.com/maps/documentation/javascript/examples/icon-complex

// The following example creates complex markers to indicate beaches near
// Sydney, NSW, Australia. Note that the anchor is set to
// (0,32) to correspond to the base of the flagpole.

function initialize() {
  var mapOptions = {
    zoom: 12,
    center: new google.maps.LatLng($lat,$lng)
  }
  var map = new google.maps.Map(document.getElementById('single-gmap'),
                                mapOptions);

  setMarkers(map, myLatlng);
}

/**
 * Data for the markers consisting of a name, a LatLng and a zIndex for
 * the order in which these markers should display on top of each
 * other.
 */
var myLatlng = [
  ['1', $lat,$lng, 4],
  ['2', 28.338166, -81.64695389999997],
  ['3', 28.31497, -81.597636],
  ['4', 28.34903251, -81.61249638],
  ['5', 28.31390193, -81.62090778],
  ['6', 28.30324714, -81.61095142],
  ['7', 28.31307075, -81.5823698],
  ['8', 28.3302221, -81.63386822],
  ['9', 28.34857928, -81.62142277]
];

function setMarkers(map, locations) {
  // Add markers to the map

  // Marker sizes are expressed as a Size of X,Y
  // where the origin of the image (0,0) is located
  // in the top left of the image.

  // Origins, anchor positions and coordinates of the marker
  // increase in the X direction to the right and in
  // the Y direction down.
  var image = {
    url: 'images/default-marker.png',
    // This marker is 20 pixels wide by 32 pixels tall.
    size: new google.maps.Size(51, 70),
    // The origin for this image is 0,0.
    origin: new google.maps.Point(0,0),
    // The anchor for this image is the base of the flagpole at 0,32.
    anchor: new google.maps.Point(0, 32)
  };

  // Shapes define the clickable region of the icon.
  // The type defines an HTML <area> element 'poly' which
  // traces out a polygon as a series of X,Y points. The final
  // coordinate closes the poly by connecting to the first
  // coordinate.
  var shape = {
      coords: [1, 1, 1, 20, 18, 20, 18 , 1],
      type: 'poly'
  };
  for (var i = 0; i < locations.length; i++) {
    var beach = locations[i];
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image,
        shape: shape,
        title: beach[0],
        zIndex: beach[3]
    });
  }
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Abaixo.. tem a função que defini o inicial, o meio e o final.. cada qual com ícone diferentes... abs:

  
    <script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var infowindow = new google.maps.InfoWindow();
		
function initialize() {
	  directionsDisplay = new google.maps.DirectionsRenderer();
	  var latlng = new google.maps.LatLng(-23.521182,-46.63559);
	  
	    var mapOpts = {
	      zoom: 8,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP,
	    };
	 
	   map = new google.maps.Map(document.getElementById("map-canvas"),
	        mapOpts);
	  //directionsDisplay.setMap(map);
	  
	  google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
	       computeTotalDistance(directionsDisplay.directions);
	  });
	  
	  calcRoute();
	  
}


function createMarker(point,info,dt,bt,map) {
    var iconURL = 'img/pata.png';				
    var iconSize = new google.maps.Size(32,34);
    var iconOrigin = new google.maps.Point(0,0);	
    var iconAnchor = new google.maps.Point(15,30);
 
    var myIcon = new google.maps.MarkerImage(iconURL, iconSize, iconOrigin, iconAnchor);
	
    var marker = new google.maps.Marker({
      position : point,
      html : info,
      map : map,
 	  icon: myIcon
    });   
    
    google.maps.event.addListener(marker, 'click', function() {
    	endereco(info,this.position,dt,bt);
    	infowindow.open(map,this);
    });

}

function createMarkerPartida(point,info,dt,bt,map) {
    var iconURL = 'img/inicial.png';				
    var iconSize = new google.maps.Size(50,50);
    var iconOrigin = new google.maps.Point(0,0);	
    var iconAnchor = new google.maps.Point(15,30);
 
    var myIcon = new google.maps.MarkerImage(iconURL, iconSize, iconOrigin, iconAnchor);

    var marker = new google.maps.Marker({
      position : point,
      html : info,
      map : map,
 	  icon: myIcon
    });
       
    google.maps.event.addListener(marker, 'click', function() {
    	endereco(info,this.position,dt,bt);
    	infowindow.open(map,this);
    });
   
}
  
function createMarkerAtual(point,info,dt,bt,map) {
    var iconURL = 'img/final.png';				
    var iconSize = new google.maps.Size(45,45);
    var iconOrigin = new google.maps.Point(0,0);	
    var iconAnchor = new google.maps.Point(15,30);
 
    var myIcon = new google.maps.MarkerImage(iconURL, iconSize, iconOrigin, iconAnchor);

    var marker = new google.maps.Marker({
      position : point,
      html : info,
      map : map,
 	  icon: myIcon
    });
    	
    google.maps.event.addListener(marker, 'click', function() {
 	  	endereco(info,this.position,dt,bt);
    	infowindow.open(map,this);
    });
    
}



 

 

 

E essa é para calcular a rota.. Pego uma lista do servlet (java, não sei o que vc ta usando):

function calcRoute() {
	var myTrip=[];
	var bounds = new google.maps.LatLngBounds();
	    <c:forEach var="listaCoord" varStatus="posicao" items="${listaCoord}">
	    	
			var dt = '${listaCoord.dtSistema}';					
			var cod = '${listaCoord.codDaf}';
			var lat = '${listaCoord.idLatitude}';
			var lng = '${listaCoord.idLongitude}';
			var bt = '${listaCoord.bateria}';
			
			var pt = new google.maps.LatLng(lat, lng);
					
			myTrip.push(pt);
			bounds.extend(pt);

			<c:choose>
				<c:when test="${posicao.first}">
					dt = dt + ' - <b>ATUAL</b>';
	    			atual = pt;
	    			createMarkerAtual(pt,cod,dt,bt,map);
		    	</c:when>
		    	
		    	<c:when test="${posicao.last}">
		    		dt = dt + ' - <b>PARTIDA</b>';
	    			inicio = pt;
	    			createMarkerPartida(pt,cod,dt,bt,map);
		    	</c:when>
		    	
		        <c:otherwise>
		        	createMarker(pt,cod,dt,bt,map);
		    	</c:otherwise>
			</c:choose>
			
	    </c:forEach>

	    var flightPath = new google.maps.Polyline({
	    	   path:myTrip,
	    	   strokeColor:"#0000FF",
	    	   strokeOpacity:0.5,
	    	   strokeWeight:4
	    	 });

	    flightPath.setMap(map);
	    map.fitBounds(bounds);
	    //distTotal(atual,inicio);
}



Compartilhar este post


Link para o post
Compartilhar em outros sites

Daria para pegar a URL da var image e declarar dentro do array var myLatlng OU simplistemnte transformar a var image em um array como o myLatlng? Agradeco.

var myLatlng = [
  ['1', $lat,$lng, 4],
  ['2', 28.338166, -81.64695389999997],
  ['3', 28.31497, -81.597636],
  ['4', 28.34903251, -81.61249638],
  ['5', 28.31390193, -81.62090778],
  ['6', 28.30324714, -81.61095142],
  ['7', 28.31307075, -81.5823698],
  ['8', 28.3302221, -81.63386822],
  ['9', 28.34857928, -81.62142277]
];

Compartilhar este post


Link para o post
Compartilhar em outros sites

Cara acho que da.. mas vamos ver se eu entendi...

 

Vc quer que "cada um" dos marcadores seja uma imagem? é isso?

 

 

Se for tente fazer um for para adicionar no array as imagens (recomendo deixar no seu projeto, pois URL pode ser que de problema e vc não vai ver...) e a cada momento de for criar vc chama a imagem nova.. acho que da sim....

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.