Write state name on svg map using jqv map

房东的猫 提交于 2019-12-13 09:51:05

问题


I have created USA states map using jqvmap . See jqvmap.com

I want to write state names in middle of map like image below.

I have tried using pseudo elements but its not working.

This is my code.

jQuery(document).ready(function () {
                jQuery('#vmap').vectorMap({
                    map: 'usa_en',
                    enableZoom: false,
                    showTooltip: true,
                    backgroundColor: '#D9D9D9',
                    color: '#009F45',
                    borderColor: '#ffffff',
                    borderOpacity: 0.25,
                    borderWidth: 2,
                    hoverColor: '#999999',
                    selectedColor: '#0077aa',
                    selectedRegion: 'MO',
                    onRegionClick: function (element, code, region)
                    {
                        var message = 'You clicked "'
                                + region
                                + '" which has the code: '
                                + code.toUpperCase();
                        alert(message);
                    }

                });

            });
/*!
 * jQVMap Version 1.0 
 *
 * http://jqvmap.com
 *
 * Copyright 2012, Peter Schmalfeldt <manifestinteractive@gmail.com>
 * Licensed under the MIT license.
 *
 * Fork Me @ https://github.com/manifestinteractive/jqvmap
 */
.jqvmap-label
{
    position: absolute;
    display: none;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    background: #292929;
    color: white;
    font-family: sans-serif, Verdana;
    font-size: smaller;
    padding: 3px;
}
.jqvmap-zoomin, .jqvmap-zoomout
{
    position: absolute;
    left: 10px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    background: #000000;
    padding: 3px;
    color: white;
    width: 10px;
    height: 10px;
    cursor: pointer;
    line-height: 10px;
    text-align: center;
}
.jqvmap-zoomin
{
    top: 10px;
}
.jqvmap-zoomout
{
    top: 30px;
}
.jqvmap-region
{
    cursor: pointer;
}
.jqvmap-ajax_response
{
    width: 100%;
    height: 500px;
}

/*Colors of state*/

path#jqvmap1_nj{
    fill:#7AC37A;
}

path#jqvmap1_tn{
    fill:#7AC37A;
}

path#jqvmap1_in{
    fill:#7AC37A;
}

path#jqvmap1_co{
    fill:#7AC37A;
}

path#jqvmap1_ca{
    fill:#026E38;
}
path#jqvmap1_ca:after{
    content:'ca';
    position: absolute;
    top: 0;
    color:#fff;
}

path#jqvmap1_ak{
    fill:#6E6F73;
}

path#jqvmap1_tx{
    fill:#6E6F73;
}

path#jqvmap1_ar{
    fill:#6E6F73;
}

path#jqvmap1_la{
    fill:#6E6F73;
}

path#jqvmap1_al{
    fill:#6E6F73;
}

path#jqvmap1_nh{
    fill:#6E6F73;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://jqvmap.com/js/vmap/jquery.vmap.js"></script>
<script src="https://jqvmap.com/js/vmap/jquery.vmap.usa.js"></script>
<div id="vmap" style="width: 600px; height: 400px;"></div>

回答1:


First, there is no built-in parameter in jqvmap to add the states code.

Finding a good position for this labels is not a trivial task. For example, Florida's shape is not convex, Michigan has two parts.

Some questions related on stackexchange network:
- Algorithm for finding irrregular polygon centroid (label point)
- How to split compound polygons into convex polygons?
- Partitioning a polygon into convex parts

So, I tried to place them with a dummy algorithm, which places the state code at a kind of centroid of state shapes. Then, you can move them as you want, and use the positions you have set.

Here is the main function which compute the centroid of a SVG path:

     //svgPathParser => browserified version of
     // https://github.com/hughsk/svg-path-parser
     var parsedPath= svgPathParser(path);

    // pathes in jqvmap are, for the most of them, in the form of [ start Point, [ curves ] ]
    // if you want better results it is possible to refine that.
    var origin= { x: parsedPath[0].x, y: parsedPath[0].y };
    var pathPartCentroids= [];
    var totalLength= 0;
    for( var i=1; i< parsedPath.length - 1; i++){

        var pathPart= parsedPath[i];

        if(pathPart.code !="c")
            break;

        //centroidOfPathPart returns the centroid of a Bezier curve. 
        var pathPartCentroid= centroidOfPathPart([ [0,0], [ pathPart.x1, pathPart.y1 ], [ pathPart.x2, pathPart.y2 ], [ pathPart.x, pathPart.y ] ]); 

        pathPartCentroid.x += origin.x;
        pathPartCentroid.y += origin.y;

        pathPartCentroid={ centroid: pathPartCentroid, distance: norm( pathPartCentroid, origin) } 
        pathPartCentroids.push(pathPartCentroid);

        totalLength+= pathPartCentroid.distance;

        origin.x+= pathPart.x;
        origin.y+= pathPart.y;
    }

    var centroid= {x:0,y:0};

    //segments are weighted by their length 
    pathPartCentroids.forEach(function( pathPart ){
        centroid.x += pathPart.centroid.x * pathPart.distance / totalLength;
        centroid.y += pathPart.centroid.y * pathPart.distance / totalLength;
    });

You can edit position with this pen.
Then use that another one to add state codes in a map.



来源:https://stackoverflow.com/questions/29966067/write-state-name-on-svg-map-using-jqv-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!