Foros del Web » Programando para Internet » Javascript »

usar json data en un google maps

Estas en el tema de usar json data en un google maps en el foro de Javascript en Foros del Web. hola quiero tomar los diferentes registros que tengo en un json en este codigo de google maps en la variable "beaches" que puedo hacer? Código: ...
  #1 (permalink)  
Antiguo 24/06/2014, 19:04
 
Fecha de Ingreso: enero-2012
Mensajes: 224
Antigüedad: 12 años, 3 meses
Puntos: 1
usar json data en un google maps

hola quiero tomar los diferentes registros que tengo en un json en este codigo de google maps en la variable "beaches" que puedo hacer?

Código:

  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
// 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: 13,
    center: new google.maps.LatLng(-34.800787, -58.388517)
  }
  var map = new google.maps.Map(document.getElementById('map-canvas'),
                                mapOptions);

  setMarkers(map, beaches);
}

/**
 * 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 beaches = [
  ['Bondi Beach', -34.797615, -58.362940, 5],
  ['Coogee Beach', -34.815515, -58.365601, 4],
  ['Cronulla Beach', -34.781813, -58.347786, 3],
  ['Manly Beach', -34.779275, -58.365295, 2],
  ['Maroubra Beach', -34.802324, -58.338860, 1]
];

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: 'wp-content/themes/brownplus/img/beachflag.png',
    // This marker is 20 pixels wide by 32 pixels tall.
    size: new google.maps.Size(32, 32),
    // 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(32, 32)
  };
  // Shapes define the clickable region of the icon.
  // The type defines an HTML &lt;area&gt; 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 = {
      coord: [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]
    });
  }
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  #2 (permalink)  
Antiguo 26/06/2014, 04:49
Avatar de dreamkarkayu  
Fecha de Ingreso: junio-2010
Mensajes: 22
Antigüedad: 13 años, 10 meses
Puntos: 2
Respuesta: usar json data en un google maps

A ver si te he entendido bien: Lo que quieres es que la variable 'beaches' del ejemplo que has puesto tenga valores de un fichero JSON tuyo, no?

Si es así puedes usar la funcion $.getJSON de JQUERY para leer el contenido del JSON y asignarlo a la variable que quieras:

Ej:
Código:
$.getJSON("fichero.json",function(datos){

         //En 'datos' tengo lo que he leido del fichero JSON
         //Puedo asignarlo a otra variable o recorrerlo y mostrarlo:
	  $.each(datos, function(clave, valor){
	      $("div").append(valor + "<br>");
	   });
});

Etiquetas: data, google, html, js, json, maps, usar, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:13.