简体   繁体   中英

Google Maps Circle

I am trying to load data from an API then display it using circles. I am able to create markers with the data points but not circles. I am following this example here from Google's documentation .

What I expect to happen is in the for loop , using center: new google.maps.LatLng(well.location.latitude, well.location.longitude) would suffice to create the center points. However, that doesn't seem to work. Everything else is the same as the example (will modify later).

I expected this to work because earlier in the example, I am able to use $.each to display markers using field.location.latitude, field.location.longitude which is essentially the same thing (or so I think).

Can I not make circles within the $.getJSON function like I can with markers? Is it happening "out of sync"? I'm still trying to learn how to process async events.

Fiddle here.

HTML:

<head>
    <script src="http://maps.googleapis.com/maps/api/js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <div id="map"></div>
</body>

CSS:

#map {
    border: 1px solid black;
    margin: 0 auto;
    width: 500px;
    height: 300px;
}

JavaScript

var map;
var mapProp;
var url;
var marker;
var markers = [];
var infoWindow;
var wellCircle;

function initMap() {
    mapProp = {
        center: new google.maps.LatLng(39.0, -105.782067),
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById("map"), mapProp);
    infoWindow = new google.maps.InfoWindow({
        content: "hello world"
    });
};

function addMarker(lat, lng) {
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, lng),
        map: map
    });
    markers.push(marker);
    //console.log(markers);
};
$(document).ready(function() {
    url = 'https://data.colorado.gov/resource/hfwh-wsgi.json?&$limit=500';
    initMap();
    $.getJSON(url, function(data) {
        //console.log(data);
        for (var i = 0; i < data.length; i++) {
            //console.log(data[i].location.latitude + ", " + data[i].location.longitude);
        };
        $.each(data, function(i, field) {
            addMarker(field.location.latitude, field.location.longitude);
        });
        for (var well in data) {
            wellCircle = new google.maps.Circle({
                strokeColor: '#FF0000',
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: '#FF0000',
                fillOpacity: 0.35,
                map: map,
                center: new google.maps.LatLng(well.location.latitude,
                    well.location.longitude),
                radius: 100000
            });
        };
    });
});

data is an array, either iterate through it, or use $.each (or .forEach).

for (var i=0; i < data.length; i++) {
    var wellCircle = new google.maps.Circle({
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        map: map,
        center: new google.maps.LatLng(data[i].location.latitude, data[i].location.longitude),
        radius: 10000
    });
};

or (like you did with the markers):

$.each(data, function(i, well) {
    var wellCircle = new google.maps.Circle({
        strokeColor: '#FF0000',
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: '#FF0000',
        fillOpacity: 0.35,
        map: map,
        center: new google.maps.LatLng(well.location.latitude, well.location.longitude),
        radius: 10000
    });
});

生成的地图的屏幕截图

code snippet:

 var map; var mapProp; function initMap() { mapProp = { center: new google.maps.LatLng(39.0, -105.782067), zoom: 6, mapTypeId: google.maps.MapTypeId.TERRAIN }; map = new google.maps.Map(document.getElementById("map"), mapProp); infoWindow = new google.maps.InfoWindow({ content: "hello world" }); }; $(document).ready(function() { url = 'https://data.colorado.gov/resource/hfwh-wsgi.json?&$limit=500'; initMap(); $.getJSON(url, function(data) { $.each(data, function(i, well) { var wellCircle = new google.maps.Circle({ strokeColor: '#FF0000', strokeOpacity: 0.8, strokeWeight: 2, fillColor: '#FF0000', fillOpacity: 0.35, map: map, center: new google.maps.LatLng(well.location.latitude, well.location.longitude), radius: 10000 }); }); }); }); 
 body, html { margin: 0px; padding: 0px; width: 100%; height: 100%; } #map { border: 1px solid black; margin: 0 auto; width: 99%; height: 99%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map"></div> 

Your code for the markers was correct, but there are some items of your data that do not have a location property, that's why your code is not fully working.

If you want to add Circles instead of markers, you can use your $.each loop and simply check the location block before adding a point.

Here is a working example: http://jsfiddle.net/xb7eh58p/ (sorry, didn't use yours because I had not seen your link)

In details, here is your code that I adjusted:

var map;
var mapProp;
var url;
var marker;
var markers = [];
var infoWindow;
var wellCircle;

function initMap() {
    mapProp = {
        center: new google.maps.LatLng(39.0, -105.782067),
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById("map"), mapProp);
    infoWindow = new google.maps.InfoWindow({
        content: "hello world"
    });
};

function addMarker(lat, lng) {
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat, lng),
        map: map
    });
    markers.push(marker);
};
$(document).ready(function() {
    url = 'https://data.colorado.gov/resource/hfwh-wsgi.json?&$limit=500';
    initMap();    
    $.getJSON(url, function(data) {
        //console.log(data);
        //for (var i = 0; i < data.length; i++) {
        //    console.log(data[i].location.latitude + ", " + data[i].location.longitude);
        //};
        $.each(data, function(i, field) {
            if(field.location) {
                 wellCircle = new google.maps.Circle({
                    strokeColor: '#FF0000',
                    strokeOpacity: 0.8,
                    strokeWeight: 2,
                    fillColor: '#FF0000',
                    fillOpacity: 0.35,
                    map: map,
                    center: new google.maps.LatLng(field.location.latitude,
                        field.location.longitude),
                    radius: 100000
                });
            } else {
                console.log("Missing location for this data item");
            }
        });
    });
});

As you can see, you just need to ckeck if(field.location)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM