简体   繁体   English

使用google maps api根据数据中的值使用不同颜色的圆圈

[英]circles with different color based on the values in the data using google maps api

I am reading the place names from a csv file and doing geocoding using google maps api.我正在从 csv 文件中读取地名并使用 google maps api 进行地理编码。 I am using circle icons instead of default red markers.我使用的是圆形图标而不是默认的红色标记。 Now i would like to know whether those circle icon can be appear in different colours based on the value in the csv data using google maps api?现在我想知道是否可以使用谷歌地图 api 根据 csv 数据中的值以不同颜色显示这些圆圈图标?

for ex i will get a json response for the region_name and average as例如,我将得到 region_name 的 json 响应,平均为

$.getJSON(json_link,
                            function (data) {
                                //console.log(data)
                                // response(data);
                                var place_names = [];
                                var average_value = [];
                                for (i=0; i<data.rows.length; i++) {
                                    unique = data.rows[i].region_name;
                                    place_names.push(unique);
                                }
                                for (i=0; i<data.rows.length; i++) {
                                    average_values = data.rows[i].average;
                                    average_value.push(average_values);
                                }
                                console.log(place_names);
                                console.log(average_value);

And those region_names will have corresponding average values which is stored in average_value array.而那些 region_names 将有相应的平均值存储在 average_value 数组中。 For those particular region_names with their corresponding average_value, i should get the color of the circle icon vary.对于那些具有相应平均值的特定区域名称,我应该使圆圈图标的颜色有所不同。

And my geocoding part is我的地理编码部分是

var geocoder = new google.maps.Geocoder();
                                var address = place_names;
                                var average = average_value;
                                var color = 'blue'; 
                                console.log(address);
                                for (i=0; i<=address.length; i++) {
                                    if (average[i] >  0 && average[i] < 30) {
                                        color = 'red';
                                    } 
                                    else {
                                        color = 'green';
                                    }
                                    geocoder.geocode({'address': address[i]}, function(results, status) {
                                        if (status === google.maps.GeocoderStatus.OK) {
                                            map.setCenter(results[0].geometry.location);
                                                var marker = new google.maps.Marker({
                                                    position: results[0].geometry.location,
                                                    icon: {
                                                        path: google.maps.SymbolPath.CIRCLE,
                                                        scale: 10,
                                                        fillColor: color,
                                                        fillOpacity: 0.5,
                                                        strokeWeight: 1
                                                    },
                                                    map: map
                                                });

                                        } else {
                                            alert('Geocode was not successful for the following reason: ' + status);
                                        }
                                    });
                                }

you can use sometinghs like this你可以使用这样的东西

     var address = place_names;
     var average =  average_value;
     var color = '#F00';
                            console.log(address);

                            for (i=0; i<=address.length; i++) {

                                 if (average[i] >  your_lower_limit && average[i] < you_upper_limit) {
                                    color = '#0F0';
                                 }
                                // repeat for other limit and color 
                                geocoder.geocode({'address': address[i]}, function(results, status) {
                                    if (status === google.maps.GeocoderStatus.OK) {
                                        map.setCenter(results[0].geometry.location);
                                        var marker = new google.maps.Marker({
                                            position: results[0].geometry.location,
                                            icon: {
                                                path: google.maps.SymbolPath.CIRCLE,
                                                scale: 10,
                                                fillColor: color,
                                                fillOpacity: 0.4,
                                                strokeWeight: 0.4
                                            },
                                            map: map
                                        });

You have a syntax error in your code, causing it to run past the end of the array:您的代码中有语法错误,导致它运行到数组末尾:

for (i = 0; i <= address.length; i++) {

should be应该

for (i = 0; i < address.length; i++) {

One way to solve your issue with colored icons is to use function closure to associate the color with the callback of the geocoder:解决彩色图标问题的一种方法是使用函数闭包将颜色与地理编码器的回调相关联:

function geocode(address, color, geocoder, map) {
    geocoder.geocode({
        'address': address
    }, function (results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            bounds.extend(results[0].geometry.location);
            var marker = new google.maps.Marker({
                position: results[0].geometry.location,
                icon: {
                    path: google.maps.SymbolPath.CIRCLE,
                    scale: 10,
                    fillColor: color,
                    fillOpacity: 0.5,
                    strokeWeight: 1
                },
                map: map
            });
            map.fitBounds(bounds);

        } else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}

proof of concept fiddle概念证明小提琴

code snippet:代码片段:

 var place_names = ["New York, NY", "Newark, NJ", "Boston, MA", "Baltimore, MD"]; var average_value = [5, 40, 20, 60]; var bounds = new google.maps.LatLngBounds(); function initialize() { var map = new google.maps.Map( document.getElementById("map_canvas"), { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }); var geocoder = new google.maps.Geocoder(); var address = place_names; var average = average_value; var color = 'blue'; console.log(address); for (i = 0; i < address.length; i++) { if (average[i] > 0 && average[i] < 30) { color = 'red'; } else { color = 'green'; } geocode(address[i], color, geocoder, map); } } google.maps.event.addDomListener(window, "load", initialize); function geocode(address, color, geocoder, map) { geocoder.geocode({ 'address': address }, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); bounds.extend(results[0].geometry.location); var marker = new google.maps.Marker({ position: results[0].geometry.location, icon: { path: google.maps.SymbolPath.CIRCLE, scale: 10, fillColor: color, fillOpacity: 0.5, strokeWeight: 1 }, map: map }); map.fitBounds(bounds); } else { alert('Geocode was not successful for the following reason: ' + status); } }); }
 html, body, #map_canvas { height: 100%; width: 100%; margin: 0px; padding: 0px }
 <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map_canvas"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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