Jvector map, how to focus on the marker?

Another annoying problem with Jvectormap, I want to focus on the marker on page / map load via lngLat, how would I do that? Ideally it would be a good idea to say focus on that marker or focus on latlng. I will only show 1 marker per map, but I will not know the x / y only lngLat or possibly the country code. It might be an easier way to do it, so suggestions would be welcome. Thanks for your help in advanced

  var markers = [ {latLng: [47.774099, -52.793427], name: "loc 1", label: "This blahblah"}]
   $(function(){
    $('#map1').vectorMap({
                  map: 'world_mill_en',
                  scale: ['#C8EEFF', '#0071A4'],
                  normalizeFunction: 'polynomial',
                  hoverOpacity: 0.7,
                  hoverColor: false,
                  markerStyle: {
                    initial: {
                          fill: '#F8E23B',
                          stroke: '#383f47'
                    }
                  },
                  backgroundColor: '#383f47',
                  markers: markers,
                  focusOn:{
                    latLng: [47.774099, -52.793427],
                    scale: 5
                  },
                  onMarkerLabelShow: function(event, label, index) {
                       label.html( ''+markers[index].label+'');            
                   }
            });
});

      

+3


source to share


1 answer


I needed the same as you and came across your unanswered question. This is the code I wrote (forward slash, copied, pasted and modified from jVectorMap source) to solve the problem for myself. Hope you and others find this helpful.

Just pass the scale, longitude, and latitude to setFocusLatLng.

I can try to get this or something similar in the jVectorMap project on GitHub, so there might be a better way to do this later.

Disclaimer: Points on the edge of the map will not be centered. However, they should be in this.



EDIT: As requested, here's what's on the jsfiddle: http://jsfiddle.net/BryanTheScott/rs7H5/

EDIT: Also added the rest of the JS below:

$(function(){
    var smallMap = $('#my_map_container').vectorMap({
        map: 'world_mill_en',
        zoomOnScroll: true,
        zoomMax:5,
        zoomMin:5,
        regionStyle: {
            initial: {
                fill: '#222222',
                "fill-opacity": 1,
                stroke: '#444444',
                "stroke-width": 1,
                "stroke-opacity": 0.7
            },
            hover: {
                "fill-opacity": 0.8,
                fill: '#333333'
            }
        },
        markerStyle: {
            initial: {
                fill: "#000000",
                "stroke": "#7FC556",
                "stroke-width": 2,
                r: 3
            }
        },
        markers: [[37.770172,-122.422771]]
    });

    var mapObj = $('#my_map_container').vectorMap('get', 'mapObject');

    mapObj.setFocusLatLng = function(scale, lat, lng){
        var point,
            proj = jvm.WorldMap.maps[this.params.map].projection,
            centralMeridian = proj.centralMeridian,
            width = this.width - this.baseTransX * 2 * this.baseScale,
            height = this.height - this.baseTransY * 2 * this.baseScale,
            inset,
            bbox,
            scaleFactor = this.scale / this.baseScale,
            centerX,
            centerY;

        if (lng < (-180 + centralMeridian)) {
            lng += 360;
        }

        point = jvm.Proj[proj.type](lat, lng, centralMeridian);

        inset = this.getInsetForPoint(point.x, point.y);
        if (inset) {
            bbox = inset.bbox;

            centerX = (point.x - bbox[0].x) / (bbox[1].x - bbox[0].x);
            centerY = (point.y - bbox[0].y) / (bbox[1].y - bbox[0].y);

            this.setFocus(scale, centerX, centerY);
        }
    }

    mapObj.setFocusLatLng(5, 37.770172,-122.422771);
});

      

+9


source







All Articles