简体   繁体   English

显示大洲地图并放大显示国家

[英]Show continent map and on zoom show country

on my web page in CONTACT section I want to add interactive google maps. 在我的网页的“联系”部分中,我想添加交互式Google地图。 Idea is, on load user can see all maps with continents 想法是,在加载时用户可以看到所有带有大洲的地图 在此处输入图片说明

And when user click on continent, maps automatically zoom in 当用户点击大洲时,地图会自动放大 在此处输入图片说明

And user can click on any of this country to get some event (in my example alert) 用户可以点击该国家/地区中的任何一个来获取一些事件(在我的示例警报中)

This is how I do this, but I don't know how to separate continents and countres and zoom in 这就是我的操作方式,但是我不知道如何分隔大洲和国家/地区并放大

 // the map var map; function initialize() { var myOptions = { zoom: 2, center: new google.maps.LatLng(10, 0), mapTypeId: google.maps.MapTypeId.ROADMAP }; // initialize the map map = new google.maps.Map(document.getElementById('map-canvas'), myOptions); // these are the map styles var styles = [ { "featureType": "administrative", "elementType": "all", "stylers": [ { "color": "#a8a8a8" } ] }, { "featureType": "administrative.country", "elementType": "geometry.fill", "stylers": [ { "visibility": "off" } ] }, { "featureType": "administrative.country", "elementType": "geometry.stroke", "stylers": [ { "visibility": "on" } ] }, { "featureType": "administrative.country", "elementType": "labels", "stylers": [ { "visibility": "off" } ] }, { "featureType": "administrative.country", "elementType": "labels.text", "stylers": [ { "visibility": "simplified" } ] }, { "featureType": "administrative.country", "elementType": "labels.text.fill", "stylers": [ { "visibility": "off" } ] }, { "featureType": "administrative.country", "elementType": "labels.icon", "stylers": [ { "visibility": "off" } ] }, { "featureType": "administrative.province", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "administrative.province", "elementType": "geometry", "stylers": [ { "visibility": "off" } ] }, { "featureType": "administrative.locality", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "administrative.neighborhood", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "administrative.land_parcel", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "landscape", "elementType": "all", "stylers": [ { "saturation": -100 }, { "lightness": 60 }, { "visibility": "on" }, { "color": "#e2e2e2" } ] }, { "featureType": "poi", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "poi.park", "elementType": "geometry.fill", "stylers": [ { "color": "#b6c54c" }, { "lightness": 40 }, { "saturation": -40 } ] }, { "featureType": "road", "elementType": "all", "stylers": [ { "visibility": "off" } ] }, { "featureType": "road.highway", "elementType": "geometry.fill", "stylers": [ { "color": "#ef8c25" }, { "lightness": 40 } ] }, { "featureType": "road.highway", "elementType": "geometry.stroke", "stylers": [ { "visibility": "off" } ] }, { "featureType": "road.local", "elementType": "all", "stylers": [ { "saturation": -100 }, { "lightness": 40 }, { "visibility": "on" } ] }, { "featureType": "transit", "elementType": "all", "stylers": [ { "saturation": -100 }, { "visibility": "off" } ] }, { "featureType": "water", "elementType": "all", "stylers": [ { "visibility": "simplified" }, { "lightness": 30 }, { "color": "#ffffff" }, { "saturation": "16" } ] }, { "featureType": "water", "elementType": "labels", "stylers": [ { "visibility": "off" } ] } ] map.setOptions({styles: styles}); // Initialize JSONP request var script = document.createElement('script'); var url = ['https://www.googleapis.com/fusiontables/v1/query?']; url.push('sql='); var query = 'SELECT name, kml_4326 FROM ' + '1foc3xO9DyfSIF6ofvN0kp2bxSfSeKog5FbdWdQ'; var encodedQuery = encodeURIComponent(query); url.push(encodedQuery); url.push('&callback=drawMap'); url.push('&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ'); script.src = url.join(''); var body = document.getElementsByTagName('body')[0]; body.appendChild(script); } function drawMap(data) { var rows = data['rows']; for (var i in rows) { if (rows[i][0] != 'Antarctica') { var newCoordinates = []; var geometries = rows[i][1]['geometries']; if (geometries) { for (var j in geometries) { newCoordinates.push(constructNewCoordinates(geometries[j])); } } else { newCoordinates = constructNewCoordinates(rows[i][1]['geometry']); } var country = new google.maps.Polygon({ paths: newCoordinates, strokeColor: '#a8a8a8', strokeOpacity: 1, strokeWeight: 0.3, fillColor: '#a8a8a8', fillOpacity: 0, name: rows[i][0] }); google.maps.event.addListener(country, 'mouseover', function() { this.setOptions({fillOpacity: 0.4}); }); google.maps.event.addListener(country, 'mouseout', function() { this.setOptions({fillOpacity: 0}); }); google.maps.event.addListener(country, 'click', function() { if(this.name =="Brazil"){ alert("Brazil"); }; if(this.name =="Croatia"){ alert("Croatia"); }; if(this.name =="Russia"){ alert("Russia"); }; }); country.setMap(map); } } } function constructNewCoordinates(polygon) { var newCoordinates = []; var coordinates = polygon['coordinates'][0]; for (var i in coordinates) { newCoordinates.push( new google.maps.LatLng(coordinates[i][1], coordinates[i][0])); } return newCoordinates; } google.maps.event.addDomListener(window, 'load', initialize); 
 #map-canvas { height: 600px; width: 800px; } 
 <div id="map-canvas"></div> 

Here is working jsfiddle 这是工作的jsfiddle

This is where I find idea AMCHARTS but I need free and custom solution 这是我找到想法AMCHARTS的地方,但我需要免费的自定义解决方案

And also, in my code, you can find this 而且,在我的代码中,您可以找到

 google.maps.event.addListener(country, 'click', function() {
      if(this.name =="Brazil"){  
      alert("Brazil");
      };
      if(this.name =="Croatia"){  
      alert("Croatia");
      };
     if(this.name =="Russia"){
      alert("Russia");
      };
    });

Why here, if I add ELSE code not working 为什么在这里,如果我添加ELSE代码不起作用

 google.maps.event.addListener(country, 'click', function() {
      if(this.name =="Brazil"){  
      alert("Brazil");
      };
      if(this.name =="Croatia"){  
      alert("Croatia");
      };
     if(this.name =="Russia"){
      alert("Russia");
      };
     else{
      alert("Send Us mail");
     }
    });

On the click-event you could just set the map.setZoom(4); 在点击事件上,您可以只设置map.setZoom(4); to get the map to zoom in. 以放大地图。

You could look in to the if (geometries) {} , I think it is there the lines will be outdrawn. 您可以查看if (geometries) {} ,我认为那里的线条会被画出来。 If you comment that out the lines disappears. 如果您将其注释掉,这些行将消失。 Set a click-event on the hole MAP and not COUNTRY like you do now. 在孔MAP上而不是像现在那样在COUNTRY上设置点击事件。 And set something like if(map.zoom === 4){ //add the country lines}; 并设置类似if(map.zoom === 4){ //add the country lines}; . Hopefully this guides you in some direction closer to the answer. 希望这可以引导您更接近答案。

The if statement is wrong. if语句错误。 Change it to else if statement like this: 将其更改为else if语句,如下所示:

if(this.name =="Brazil"){ 
    alert("Brazil"); 
} else if(this.name =="Croatia"){ 
    alert("Croatia"); 
} else if(this.name =="Russia"){ 
    alert("Russia"); 
} else { 
    alert("Send Us mail"); 
}

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

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