gpt4 book ai didi

google-maps - 使用 Google map API 的可点击国家/地区

转载 作者:行者123 更新时间:2023-12-03 00:38:50 24 4
gpt4 key购买 nike

我刚刚开始涉足 Google Maps API,但我已经陷入困境。我正在寻找一种重新创建 this 的方法谷歌地图的 map 类型。我需要删除所有标签,获得空白背景(我尝试使用 map 样式,但这对我不起作用,下面的代码示例)并在我将鼠标悬停在国家/地区上方时点亮它们。

我在搜索中似乎错过了任何可以帮助我的教程,或者有人可以为我指出正确的方向吗? :)

var _mapstyle = [
{
featureType: "all",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
];

function create_map()
{
_map = new google.maps.Map(document.getElementById("eyewebz-map"),
{
zoom: 2,
center: new google.maps.LatLng(20, 0),
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.DEFAULT
},
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
scaleControl: true,
mapTypeIds: ['_mapstyle']
});

_display.setMap(_map);
_display.setPanel(document.getElementById("NavigationText"));
}

编辑这就是 map 的用途:我一生中旅行过一些地方,并希望能做更多的事情。自从我第一次长途旅行以来,我一直在写博客。我现在开发了一个新博客,我想让我去过的国家/地区可点击,并在点击后跟踪一个网址。理想的情况是,当我单击一个国家/地区时,它会将我带到一个页面,其中 map 中仅显示该特定国家/地区,并带有一些标记(要访问的地点)。单击这些标记后,它应该显示特定的帖子/一些信息。

最佳答案

这样的事情可以吗?复制并粘贴到 HTML 页面正文 ( JSFiddler here ) 中。

<style type="text/css">
#map-canvas {
height: 600px;
width: 800px;
}
</style>

<script type="text/javascript"
src="https://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">
// 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 = [
{
stylers: [
{ hue: "#00ffe6" },
{ saturation: -20 }
]
},
{
featureType: "landscape",
stylers: [
{ hue: "#ffff66" },
{ saturation: 100 }
]
},{
featureType: "road",
stylers: [
{ visibility: "off" }
]
},{
featureType: "administrative.land_parcel",
stylers: [
{ visibility: "off" }
]
},{
featureType: "administrative.locality",
stylers: [
{ visibility: "off" }
]
},{
featureType: "administrative.neighborhood",
stylers: [
{ visibility: "off" }
]
},{
featureType: "administrative.province",
stylers: [
{ visibility: "off" }
]
},{
featureType: "landscape.man_made",
stylers: [
{ visibility: "off" }
]
},{
featureType: "landscape.natural",
stylers: [
{ visibility: "off" }
]
},{
featureType: "poi",
stylers: [
{ visibility: "off" }
]
},{
featureType: "transit",
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: '#ff9900',
strokeOpacity: 1,
strokeWeight: 0.3,
fillColor: '#ffff66',
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() {
alert(this.name);
});

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);
</script>

<div id="map-canvas"></div>

这是此版本的修改版本 Fusion Tables Layer Example: Mouseover Map Styles .

您还应该查看Styled Maps .

您可能感兴趣的另一件事是 Natural Earth Data 。这是在您需要多边形数据源的情况下。这是使用它的示例 GViz API Example: Fusion Tables Data Source .

关于google-maps - 使用 Google map API 的可点击国家/地区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36866239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com