Search code examples
google-mapsmarkerclusterer

How can I get access to lng/lat when I load point data with loadGeoJson?


I am loading n amount of positional data with Google Map loadGeoJson and everything works perfectly except the data itself isn't great. Often the gps coordinates places a marker at the same position and this breaks Markerclusterer. As an experiment I decided I would add a little jitter to each marker that wont affect location too much but allow markerclusterer to do it thang.

The problem is I can't access the gps data and can't figure out how to, I checked googles API docs but no luck.

How can I get access the gps data and make changes to it before it's set to the marker?

Partial code

      map.data.loadGeoJson('mapdata.json, null, function (features) {

        var bounds = new google.maps.LatLngBounds();
        markers = features.map(function (feature) {

          var markerJitter = feature.getGeometry().get(0); //tried to access data here
          var test = markerJitter.lat; this fails too as [[scope]] is in place

          var infoWin = new google.maps.InfoWindow();
          var marker = new google.maps.Marker({
            position: feature.getGeometry().get(0),   //jitter should be present here
            title: feature.getProperty("address")
          });

          bounds.extend(marker.position);

          google.maps.event.addListener(marker, 'click', function (evt) {
            closeOtherInfo();
            infoWin.setContent('<div id="content">some content</div>');
            infoWin.open(map, marker);
            infoWindowObj[0] = infoWin;
          })
          return marker;
        }); 

        .... //code continues and works

enter image description here


Solution

  • You are accessing the google.maps.LatLng object that contains the coordinates (that is markerJitter).

    It doesn't have a .lat property, it has a .lat() function.

    I'm not sure what your comment this fails too as [[scope]] is in place means.

    That is a function. Your code is setting test equal to a function, which is probably not what you want to do.

    proof of concept fiddle

    code snippet:

    function initialize() {
      // Create a simple map.
      features = [];
      map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 10,
        center: {
          lat: 45.495403,
          lng: -73.563032
        }
      });
      google.maps.event.addListener(map, 'click', function() {
        infowidow.close();
      });
      // process the loaded GeoJSON data.
      // google.maps.event.addListener(map.data, 'addfeature', // );
      var features = map.data.addGeoJson(data);
      map.data.setMap(null);
      var bounds = new google.maps.LatLngBounds();
      markers = features.map(function(feature) {
    
        var markerJitter = feature.getGeometry().get(0); //tried to access data here
        console.log(markerJitter.toUrlValue(6));
        var test = markerJitter.lat; // this fails too as [[scope]] is in place
        console.log("markerJitter lat=" + markerJitter.lat() + " lng=" + markerJitter.lng());
        var infoWin = new google.maps.InfoWindow();
        var marker = new google.maps.Marker({
          position: feature.getGeometry().get(0), //jitter should be present here
          title: feature.getProperty("address"),
          map: map
        });
    
        bounds.extend(marker.position);
    
        google.maps.event.addListener(marker, 'click', function(evt) {
          infoWin.setContent('<div id="content">some content<br>' + this.getTitle() + '</div>');
          infoWin.open(map, marker);
          infoWindowObj[0] = infoWin;
        })
        return marker;
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    var data = {
      "type": "FeatureCollection",
      "features":
    
        [
    
          {
            "type": "Feature",
            "geometry": {
              "type": "Point",
              "coordinates": [-73.563032, 45.495403]
            },
            "properties": {
              "prop0": "value0",
              "address": "45.495403,-73.563032"
            }
          },
    
          {
            "type": "Feature",
            "geometry": {
              "type": "Point",
              "coordinates": [-73.549762, 45.559673]
            },
            "properties": {
              "prop0": "value0",
              "address": "45.559673,-73.549762"
            }
          }
        ]
    
    };
    html,
    body,
    #map-canvas {
      height: 100%;
      margin: 0px;
      padding: 0px;
      width: 100%;
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="map-canvas"></div>