Do not draw google.map.Circle in google.visualization.Map

0

I am developing a web application in laravel and I must visualize a map with markers, circles, etc. I also use google charts and I have some tables. When I click on a table I select the marker whose position equals the position I have saved in the table (that's why google.visualiazation.Map is perfect for me to do that). The problem is that when drawing circles on the map I can not do it on google.visualization.Map, I can only on the api google.maps.Map. What exactly is the difference between the two?

This is my js code that I have in the view and it does not write me any circle:

    var geo = {!! $geofences !!}
    var dev = {!! $devices !!}

    // Create our data table out of JSON data loaded from server.
    var GeofencesTable = new google.visualization.DataTable();

    GeofencesTable.addColumn('string', 'name');
    GeofencesTable.addColumn('number', 'radius');
    GeofencesTable.addColumn('string', 'latitude');
    GeofencesTable.addColumn('string', 'longitude');
    GeofencesTable.addColumn('string', 'emails');
    GeofencesTable.addColumn('string', 'created_at');
    GeofencesTable.addColumn('string', 'updated_at');

    for (var i = 0; i < geo.length; i++) {
        GeofencesTable.addRows([
            [geo[i].name, geo[i].radius, geo[i].latitude, geo[i].longitude, geo[i].emails, geo[i].created_at, geo[i].updated_at]
        ]);
    }

    var tableGeo = new google.visualization.Table(document.getElementById('table_geo'));
    tableGeo.draw(GeofencesTable, {showRowNumber: false});

    // Create our data table out of JSON data loaded from server.
    var DevicesTable = new google.visualization.DataTable();

    DevicesTable.addColumn('string', 'name');
    DevicesTable.addColumn('string', 'alias');
    DevicesTable.addColumn('number', 'battery');
    DevicesTable.addColumn('string', 'latitude');
    DevicesTable.addColumn('string', 'longitude');
    DevicesTable.addColumn('string', 'created_at');
    DevicesTable.addColumn('string', 'updated_at');

    var mapData = [['Lat', 'Lon', 'Description']];

    for (var i = 0; i < dev.length; i++) {
        DevicesTable.addRows([
            [dev[i].name, dev[i].alias, dev[i].battery, dev[i].latitude, dev[i].longitude, dev[i].created_at, dev[i].updated_at]
        ]);
        mapData.push([parseFloat(dev[i].latitude), parseFloat(dev[i].longitude), 'Battery: ' + dev[i].battery + ', Last movement: ' + dev[i].last_movement]);
    }

    var tableDev = new google.visualization.Table(document.getElementById('table_dev'));
    tableDev.draw(DevicesTable, {showRowNumber: false});

    var Info = google.visualization.arrayToDataTable(mapData);

    var geoView = new google.visualization.DataView(Info);

    var options = {
        showTooltip: true,
        showInfoWindow: true,
        useMapTypeControl: true,
        icons: {
        default: {
            normal: 'http://icons.iconarchive.com/icons/paomedia/small-n-flat/48/map-marker-icon.png',
            selected: 'http://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/Map-Marker-Marker-Outside-Azure-icon.png'
        }

        },
    };

    var map = new google.visualization.Map(document.getElementById('map_div'));
    map.draw(geoView, options);

    google.visualization.events.addListener(tableDev, 'select',
        function() {
            map.setSelection(tableDev.getSelection());
        });

    google.visualization.events.addListener(tableGeo, 'select',
        function() {
            var circle = new google.maps.Circle({
            map: map,
            center: new google.maps.LatLng(41, -1),
            radius: 500000
            });
        });

    google.visualization.events.addListener(map, 'select',
        function() {
            tableDev.setSelection(map.getSelection());
        });

Why does not the circle draw me?

Thanks in advance

    
asked by Xim123 13.09.2018 в 09:19
source

1 answer

2

According to me, the map does not need a DataView , but just the output of arrayToDataTable

var Info = google.visualization.arrayToDataTable(mapData);

var options = {
    showTooltip: true,
    showInfoWindow: true,
    useMapTypeControl: true,
    icons: {
    default: {
        normal: 'http://icons.iconarchive.com/icons/paomedia/small-n-flat/48/map-marker-icon.png',
        selected: 'http://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/Map-Marker-Marker-Outside-Azure-icon.png'
    }

    },
};

var map = new google.visualization.Map(document.getElementById('map_div'));
map.draw(Info, options);

But anyway, to draw a circle, it's not useful to assign you as a map an instance of google.visualization.Map . It has to be an instance of google.maps.Map .

With your focus, the most you can do in your case is to create a custom type of marker (with an icon that is a circle) and assign that type of marker to certain rows.

For example:

var coords = [
            ['Lat', 'Long', 'Description', 'Marker'],
            [37.4232, -122.0853, 'Work', 'blue'],
            [37.4289, -122.1697, 'University', 'green'],
            [37.6153, -122.3900, 'Airport', 'pink'],
            [37.4422, -122.1731, 'Shopping', null]
        ];
        var url = 'https://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/';


        var Info = google.visualization.arrayToDataTable(coords);

        var options = {
            zoomLevel: 10,
            showTooltip: true,
            showInfoWindow: true,
            useMapTypeControl: true,
            icons: {
                blue: {
                    normal: url + 'Map-Marker-Ball-Azure-icon.png',
                    selected: url + 'Map-Marker-Ball-Right-Azure-icon.png'
                },
                green: {
                    normal: url + 'Map-Marker-Push-Pin-1-Chartreuse-icon.png',
                    selected: url + 'Map-Marker-Push-Pin-1-Right-Chartreuse-icon.png'
                },
                pink: {
                    normal: url + 'Map-Marker-Ball-Pink-icon.png',
                    selected: url + 'Map-Marker-Ball-Right-Pink-icon.png'
                }
            }
        };
        var geoView = new google.visualization.DataView(Info);
        map = new google.visualization.Map(document.getElementById('chart_div'));

        map.draw(geoView, options);

And in the event select add an element to coords , re-declare info, re-declare geoView and call back map.draw() with the new geoView .

A second option , is that you include the Google Maps API script, so that the google.maps object is in your global scope, and draw the map yourself, using new google.maps.Map(<container>,<options>) and then adding the markers one by one instantiating new google.maps.Marker . The bad thing about this approach is that by clicking on rows in the table the map will not react, nor vice versa. They will not be binded together.

You'll have to give an identifier to your markers that matches the ID of each row of your datatable, and manipulate the states so that by clicking on the table the style of the markers whose property id matches with the id is modified of the row that you clicked, and vice versa.

I leave you an example working and, if you notice, it is quite artisanal to binde the events between the map and the datatable:

link

Eye, you need an API key for the map. The one in the example only works in JSFiddle.

    
answered by 13.09.2018 / 13:29
source