2016-06-14 471 views
-2

我試圖在美國之上實現自定義圖像。 基本上,我只需要美國顯示+覆蓋圖像就可以了。美國gmaps地圖集成

我可以工作的是,100%確切地說我需要的是芬蘭。 我無法理解如何調整它,以相同的方式工作,但對於美國。

var overlay; 
USGSOverlay.prototype = new google.maps.OverlayView(); 

// Initialize the map and the custom overlay. 

var myLatlng = new google.maps.LatLng(64.8, 24.8) 
var myLatlng2 = new google.maps.LatLng(60, 24); 
function initialize() { 
    var mapOptions = { 
    zoom: 5, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    backgroundColor: '#FFF', 
    disableDefaultUI: true, 
    draggable: false, 
    scaleControl: false, 
    scrollwheel: false, 

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

    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 

    geocoder = new google.maps.Geocoder(); 
    function codeAddress(address) { 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
     } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
     } 
    }); 
    } 

    codeAddress('Turku') 
    codeAddress('Naantali') 
    codeAddress('Oulu') 
    codeAddress('Imatra') 
    codeAddress('Mannerheimintie 2, Helsinki') 

    var swBound = new google.maps.LatLng(59.8, 19.3); 
    var neBound = new google.maps.LatLng(69.0, 31.2); 
    var bounds = new google.maps.LatLngBounds(swBound, neBound); 

    var srcImage = 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Finland_Regions_Map.svg/2000px-Finland_Regions_Map.svg.png'; 

    overlay = new USGSOverlay(bounds, srcImage, map); 
} 
// [END region_initialization] 

// [START region_constructor] 
/** @constructor */ 
function USGSOverlay(bounds, image, map) { 

    // Initialize all properties. 
    this.bounds_ = bounds; 
    this.image_ = image; 
    this.map_ = map; 

    // Define a property to hold the image's div. We'll 
    // actually create this div upon receipt of the onAdd() 
    // method so we'll leave it null for now. 
    this.div_ = null; 

    // Explicitly call setMap on this overlay. 
    this.setMap(map); 
} 
// [END region_constructor] 

// [START region_attachment] 
/** 
* onAdd is called when the map's panes are ready and the overlay has been 
* added to the map. 
*/ 
USGSOverlay.prototype.onAdd = function() { 

    var div = document.createElement('div'); 
    div.style.borderStyle = 'none'; 
    div.style.borderWidth = '0px'; 
    div.style.position = 'absolute'; 

    // Create the img element and attach it to the div. 
    var img = document.createElement('img'); 
    img.src = this.image_; 
    img.style.width = '100%'; 
    img.style.height = '100%'; 
    img.style.position = 'absolute'; 
    div.appendChild(img); 

    this.div_ = div; 

    // Add the element to the "overlayLayer" pane. 
    var panes = this.getPanes(); 
    panes.overlayLayer.appendChild(div); 
}; 
// [END region_attachment] 

// [START region_drawing] 
USGSOverlay.prototype.draw = function() { 

    // We use the south-west and north-east 
    // coordinates of the overlay to peg it to the correct position and size. 
    // To do this, we need to retrieve the projection from the overlay. 
    var overlayProjection = this.getProjection(); 

    // Retrieve the south-west and north-east coordinates of this overlay 
    // in LatLngs and convert them to pixel coordinates. 
    // We'll use these coordinates to resize the div. 
    var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest()); 
    var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast()); 

    // Resize the image's div to fit the indicated dimensions. 
    var div = this.div_; 
    div.style.left = sw.x + 'px'; 
    div.style.top = ne.y + 'px'; 
    div.style.width = (ne.x - sw.x) + 'px'; 
    div.style.height = (sw.y - ne.y) + 'px'; 
}; 
// [END region_drawing] 

// [START region_removal] 
// The onRemove() method will be called automatically from the API if 
// we ever set the overlay's map property to 'null'. 
USGSOverlay.prototype.onRemove = function() { 
    this.div_.parentNode.removeChild(this.div_); 
    this.div_ = null; 
}; 
// [END region_removal] 

google.maps.event.addDomListener(window, 'load', initialize); 

https://jsfiddle.net/gvvy5vxz/2/

提前感謝!

+1

請嘗試在問題本身中包含所有內容,您可以編輯它以包含小提琴。評論僅僅用於「評論」 –

+0

您可能會從[image]開始(https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Finland_Regions_Map.svg/2000px-Finland_Regions_Map.svg .png)。 – geocodezip

+0

@geocodezip那不是問題... –

回答

0

我自己找到了解決方案。 我發佈工作的JS代碼,謝謝!

<script> 

var overlay; 
USGSOverlay.prototype = new google.maps.OverlayView(); 

// Initialize the map and the custom overlay. 

var myLatlng = new google.maps.LatLng(39, -95) 
var myLatlng2 = new google.maps.LatLng(60, 24); 
function initialize() { 
    var mapOptions = { 
    zoom: 4, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    backgroundColor: '#FFF', 
    disableDefaultUI: true, 
    draggable: false, 
    scaleControl: false, 
    scrollwheel: false, 

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

    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 

    geocoder = new google.maps.Geocoder(); 
    function codeAddress(address,personName) { 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location, 
      title: personName 
     }); 
     } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
     } 
    }); 
    } 


    codeAddress('23361 El Toro Rd, 219 Lake Forest, CA 92630','23361 El Toro Rd, 219 Lake Forest, CA 92630') 
    codeAddress('4634 Longfellow Dr New Orleans, LA 70127','4634 Longfellow Dr New Orleans') 
    codeAddress('70 Main St,New Hampton, NH 03256','70 Main St,New Hampton, NH 03256') 
    codeAddress('1701 Broadway,Seattle, WA 98122','1701 Broadway,Seattle, WA 98122') 


    var swBound = new google.maps.LatLng(24.5, -125.2); 
    var neBound = new google.maps.LatLng(49.4, -66.8); 
    var bounds = new google.maps.LatLngBounds(swBound, neBound); 

    var srcImage = 'http://bootstrap.mapsmith.net/img/USA-Mercator.svg'; 
    //var srcImage = 'https://i.imgsafe.org/106e093026.png'; 


    overlay = new USGSOverlay(bounds, srcImage, map); 
} 
// [END region_initialization] 

// [START region_constructor] 
/** @constructor */ 
function USGSOverlay(bounds, image, map) { 

    // Initialize all properties. 
    this.bounds_ = bounds; 
    this.image_ = image; 
    this.map_ = map; 

    // Define a property to hold the image's div. We'll 
    // actually create this div upon receipt of the onAdd() 
    // method so we'll leave it null for now. 
    this.div_ = null; 

    // Explicitly call setMap on this overlay. 
    this.setMap(map); 
} 
// [END region_constructor] 

// [START region_attachment] 
/** 
* onAdd is called when the map's panes are ready and the overlay has been 
* added to the map. 
*/ 
USGSOverlay.prototype.onAdd = function() { 

    var div = document.createElement('div'); 
    div.style.borderStyle = 'none'; 
    div.style.borderWidth = '0px'; 
    div.style.position = 'absolute'; 

    // Create the img element and attach it to the div. 
    var img = document.createElement('img'); 
    img.src = this.image_; 
    img.style.width = '100%'; 
    img.style.height = '100%'; 
    img.style.position = 'absolute'; 
    div.appendChild(img); 

    this.div_ = div; 

    // Add the element to the "overlayLayer" pane. 
    var panes = this.getPanes(); 
    panes.overlayLayer.appendChild(div); 
}; 
// [END region_attachment] 

// [START region_drawing] 
USGSOverlay.prototype.draw = function() { 

    // We use the south-west and north-east 
    // coordinates of the overlay to peg it to the correct position and size. 
    // To do this, we need to retrieve the projection from the overlay. 
    var overlayProjection = this.getProjection(); 

    // Retrieve the south-west and north-east coordinates of this overlay 
    // in LatLngs and convert them to pixel coordinates. 
    // We'll use these coordinates to resize the div. 
    var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest()); 
    var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast()); 

    // Resize the image's div to fit the indicated dimensions. 
    var div = this.div_; 
    div.style.left = sw.x + 'px'; 
    div.style.top = ne.y + 'px'; 
    div.style.width = (ne.x - sw.x) + 'px'; 
    div.style.height = (sw.y - ne.y) + 'px'; 
}; 
// [END region_drawing] 

// [START region_removal] 
// The onRemove() method will be called automatically from the API if 
// we ever set the overlay's map property to 'null'. 
USGSOverlay.prototype.onRemove = function() { 
    this.div_.parentNode.removeChild(this.div_); 
    this.div_ = null; 
}; 
// [END region_removal] 

google.maps.event.addDomListener(window, 'load', initialize); 

    </script>