2010-10-26 22 views
23

Tôi tìm thấy một đoạn mã trên http://www.41latitude.com/post/1268734799/google-styled-maps:Google Maps v3 ẩn các yếu tố (đường giao thông, roadsigns, vv)

[ 
    { 
    featureType: "administrative", 
    elementType: "labels", 
    stylers: [ 
     { visibility: "off" } 
    ] 
    },{ 
    featureType: "poi", 
    elementType: "labels", 
    stylers: [ 
     { visibility: "off" } 
    ] 
    },{ 
    featureType: "water", 
    elementType: "labels", 
    stylers: [ 
     { visibility: "off" } 
    ] 
    },{ 
    featureType: "road", 
    elementType: "labels", 
    stylers: [ 
     { visibility: "off" } 
    ] 
    } 
] 

tôi sẽ có thể sử dụng nó trong các bản đồ của tôi, nhưng là có một ai đó có thể cho tôi biết thế nào Tôi có thể sử dụng đoạn mã này? Tôi không thể tìm thấy bất kỳ điều gì về nó trong API của Google Maps V3.

+0

Tôi thực sự không thấy nhiều trên trang, có vẻ như họ đang sử dụng các đường cơ sở khác nhau, satelite , vv Họ thao tác các tùy chọn tạo kiểu như đã được chi tiết. – Drew

+0

Nó ở đây https://developers.google.com/maps/documentation/javascript/styling – BigJ

Trả lời

33

Như @ceejayoz suggested in the other answer, bạn đang cố gắng sử dụng mới Styled Map features của v3 Maps API. Dưới đây là một ví dụ rất cơ bản về cách bạn có thể sử dụng phong cách trên trong một bản đồ đơn giản:

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps Dark Water Style Demo</title> 
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
</head> 
<body> 
    <div id="map" style="width: 550px; height: 300px;"></div> 

    <script type="text/javascript"> 
    var myStyle = [ 
     { 
     featureType: "administrative", 
     elementType: "labels", 
     stylers: [ 
      { visibility: "off" } 
     ] 
     },{ 
     featureType: "poi", 
     elementType: "labels", 
     stylers: [ 
      { visibility: "off" } 
     ] 
     },{ 
     featureType: "water", 
     elementType: "labels", 
     stylers: [ 
      { visibility: "off" } 
     ] 
     },{ 
     featureType: "road", 
     elementType: "labels", 
     stylers: [ 
      { visibility: "off" } 
     ] 
     } 
    ]; 

    var map = new google.maps.Map(document.getElementById('map'), { 
     mapTypeControlOptions: { 
     mapTypeIds: ['mystyle', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN] 
     }, 
     center: new google.maps.LatLng(30, 0), 
     zoom: 3, 
     mapTypeId: 'mystyle' 
    }); 

    map.mapTypes.set('mystyle', new google.maps.StyledMapType(myStyle, { name: 'My Style' })); 
    </script> 
</body> 
</html> 

Ảnh chụp màn hình:

alt text

Bạn cũng có thể muốn kiểm tra Google Maps APIs Styling Wizard mà sẽ cho phép bạn chỉnh sửa đồ họa theo phong cách.

+0

Tuyệt vời, ví dụ này là một ví dụ tốt và đã cho tôi giải pháp. –

+0

Có thể thực hiện một việc như sau: 'featureType: [" poi "," transit "]' tức là chọn tất cả và sau đó tắt hiển thị cho tất cả chúng cùng một lúc? – user2019515

+0

Có, bạn có thể sử dụng 'featureType:" all "' – GreatBittern

2

Tôi biết điều này là 5 tuổi, nhưng tôi tình cờ gặp vấn đề này và giải pháp được chấp nhận phức tạp hơn nhiều so với nhu cầu của tôi. Với JSON trong bài đăng gốc, đây là cách bạn sẽ áp dụng kiểu cho bản đồ hiện có:

var mapStyle = [ 
    { 
    featureType: "administrative", 
    elementType: "labels", 
    stylers: [ 
     { visibility: "off" } 
    ] 
    },{ 
    featureType: "poi", 
    elementType: "labels", 
    stylers: [ 
     { visibility: "off" } 
    ] 
    },{ 
    featureType: "water", 
    elementType: "labels", 
    stylers: [ 
     { visibility: "off" } 
    ] 
    },{ 
    featureType: "road", 
    elementType: "labels", 
    stylers: [ 
     { visibility: "off" } 
    ] 
    } 
] 

//create map 
var map = new google.maps.Map(...); //This assumes you already have a working map 

//set style 
map.set('styles', mapStyle); 
Các vấn đề liên quan