2015-04-23 113 views
-1

我試圖在地圖上獲取範圍環,圖像位置位於用戶位置的上方,但地圖在測試時未顯示,用戶的位置也沒有顯示似乎在地圖上顯示。我不知道出了什麼問題,我在網站上跟着一個教程。 這是代碼:地理位置Google地圖不能正常工作

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <title>Radar</title> 
 
\t <meta http-equiv="refresh" content="300"> 
 
     <meta charset="UTF-8"> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> 
 
<style> 
 
\t #map-canvas { 
 
\t \t height: 700px; 
 
\t \t } 
 
\t #logo { 
 
\t \t position: fixed; 
 
\t \t z-index: 99; 
 
\t \t top: 8%; 
 
\t \t left: 3%; 
 
\t \t opacity: 0.9; 
 
\t } 
 
\t #legenda { 
 
\t \t position: absolute; 
 
\t \t z-index: 98; 
 
\t \t top: 87%; 
 
\t \t left: 82%; 
 
    \t \t opacity: 1.0; 
 
\t \t height: 50px; 
 
\t } 
 
     </style> 
 
    </head> 
 
    <body style="overflow: hidden;"> 
 

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

 
     <script src="http://code.jquery.com/jquery-latest.js"></script> 
 
     <script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
 
     <script type="text/javascript"> 
 
// [START region_initialization] 
 
// This example creates a custom overlay called USGSOverlay, containing 
 
// a U.S. Geological Survey (USGS) image of the relevant area on the map. 
 

 
// Set the custom overlay object's prototype to a new instance 
 
// of OverlayView. In effect, this will subclass the overlay class. 
 
// Note that we set the prototype to an instance, rather than the 
 
// parent class itself, because we do not wish to modify the parent class. 
 

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

 
// Initialize the map and the custom overlay. 
 

 
function initialize() { 
 

 
if (navigator.geolocation) { 
 
    navigator.geolocation.getCurrentPosition(success); 
 
} else { 
 
    error('Geo Location is not supported'); 
 
} 
 

 

 

 
function success(position) { 
 
    var lat = position.coords.latitude; 
 
    var long = position.coords.longitude; 
 
} 
 

 
var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
 
    
 

 
var styles = 
 
[ { "featureType": "administrative", "elementType": "geometry.fill", "stylers": [ { "lightness": -88 }, { "saturation": -100 }, { "visibility": "on" } ] },{ "featureType": "administrative.country", "stylers": [ { "visibility": "on" }, { "weight": 1.3 }, { "lightness": 100 } ] },{ "featureType": "landscape", "stylers": [ { "saturation": -100 }, { "lightness": -81 } ] },{ "featureType": "poi", "stylers": [ { "visibility": "off" } ] },{ "featureType": "road.local", "stylers": [ { "visibility": "off" } ] },{ "featureType": "road.arterial", "elementType": "geometry", "stylers": [ { "visibility": "off" } ] },{ "featureType": "road.highway.controlled_access", "stylers": [ { "visibility": "simplified" } ] },{ "featureType": "road.highway.controlled_access", "elementType": "geometry.fill", "stylers": [ { "visibility": "simplified" }, { "saturation": -100 }, { "lightness": 100 }, { "weight": 1.3 } ] },{ "featureType": "road.highway", "stylers": [ { "visibility": "simplified" } ] },{ "featureType": "administrative.locality", "elementType": "labels", "stylers": [ { "lightness": 100 }, { "visibility": "simplified" } ] },{ "featureType": "road.highway", "elementType": "geometry.fill", "stylers": [ { "visibility": "simplified" }, { "lightness": 100 } ] },{ "featureType": "administrative.province", "elementType": "geometry.stroke", "stylers": [ { "lightness": 100 }, { "saturation": -100 } ] },{ "featureType": "administrative.locality", "elementType": "labels.icon", "stylers": [ { "visibility": "off" } ] },{ "featureType": "road", "elementType": "geometry", "stylers": [ { "lightness": -65 }, { "saturation": 1 }, { "hue": "#0000ff" } ] },{ "featureType": "water", "stylers": [ { "saturation": -53 }, { "lightness": -36 }, { "hue": "#00f6ff" } ] },{ "featureType": "landscape", "stylers": [ { "lightness": -39 } ] },{ } ] 
 

 
    // Create a new StyledMapType object, passing it the array of styles, 
 
    // as well as the name to be displayed on the map type control. 
 
    var styledMap = new google.maps.StyledMapType(styles, 
 
    {name: "Styled Map"}); 
 

 

 

 
    var mapOptions = { 
 
    zoom: 7, 
 
    center: coords, 
 
panControl: false, 
 
    zoomControl: false, 
 
\t mapTypeControl: false, 
 
\t streetViewControl: false, 
 
      mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style'] 
 

 

 
    }; 
 

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

 
map.mapTypes.set('map_style', styledMap); 
 
    map.setMapTypeId('map_style'); 
 

 

 

 
var icon = { 
 
    url: 'met.nl.eu.org/klanten/python/marker.png' 
 
}; 
 

 
    var marker = new google.maps.Marker({ 
 
     position: coords, 
 
     map: map, 
 
     icon: icon 
 
    }); 
 

 

 

 
       // Query and plot the data once the map is ready 
 
       google.maps.event.addListenerOnce(map, 'idle', function() { 
 

 
        $.ajax({ 
 
         dataType: "json", 
 
         url: "http://met.nl.eu.org/klanten/python/get_lightnings.php" 
 
        }).done(function(data) { 
 

 
         data.d.forEach(function(lightning) { 
 
\t \t \t \t 
 
\t \t \t \t var image = 'http://met.nl.eu.org/klanten/python/strike.png' 
 
          new google.maps.Marker({ 
 
           position: new google.maps.LatLng(lightning.lat, lightning.lon), 
 
           map: map, 
 
\t \t \t \t icon: image }); 
 
\t \t }); 
 
\t }); 
 
\t }); 
 

 
var swBound = new google.maps.LatLng(48.895311, 0.000000); 
 
var neBound = new google.maps.LatLng(55.973607, 10.856428); 
 
    var bounds = new google.maps.LatLngBounds(swBound, neBound); 
 

 
    // The photograph is courtesy of MetNL. 
 
    var srcImage = 'v2.0/historie/28-06-11/00.png'; 
 

 
    // The custom USGSOverlay object contains the USGS image, 
 
    // the bounds of the image, and a reference to the map. 
 
    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> 
 
\t <img src="http://met.nl.eu.org/NL_nl/iframe/logo.png" id="logo"/> 
 
\t <img src="http://met.nl.eu.org/klanten/python/legenda.png" id="legenda"/> 
 
    </body> 
 
</html>

笏去錯我的代碼?

**編輯:現在知道這個錯誤發生在這段代碼中:var coords = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 它提供了一個參考錯誤,說「位置沒有定義」,而在我寫的代碼的早些時候:函數成功(位置){var lat = position.coords.latitude; var long = position.coords.longitude; }

+0

' Uncaught ReferenceError:position is not defined' – geocodezip

+0

這是什麼意思? –

回答

1

地理定位異步運行。

您可以在返回結果時創建地圖/標記,或者在返回結果時定義默認座標並更新地圖/標記。

第二種方法更可取,因爲地理定位失敗時根本無法獲得地圖。

一個簡單的實現使用MVCObject,這使得它易於接入1.值和2觀察的變化(I已經刪除了不相關部分):

function initialize() { 

    //define a default-position 
    var coords = new google.maps.MVCObject(); 
    coords.set('latlng', new google.maps.LatLng(52.370215, 4.895167)); 

    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(success); 
    } 


    //set new value for coords 
    function success(position) { 
     coords.set('latlng', 
      new google.maps.LatLng(position.coords.latitude, 
       position.coords.longitude)); 
    } 

    var mapOptions = { 
     zoom: 7, 
     center: coords.get('latlng') 
    }; 

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

    var marker = new google.maps.Marker({ 
     position: coords.get('latlng'), 
     map: map 
    }); 

    //observe the latlng-property of coords, 
    //and update marker and map-center when it changes 
    google.maps.event.addListenerOnce(coords, 'latlng_changed', function() { 
     var latlng = this.get('latlng'); 
     map.setCenter(latlng); 
     marker.setPosition(latlng) 
    }); 
} 

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

演示:http://jsfiddle.net/doktormolle/rttvLsLs/

+0

Thnx。該腳本按原樣運行,但是當我將其添加到腳本的其餘部分時,它會突然停止並且頁面變爲空白。腳本中是否還有其他可能會阻止地理位置服務的內容? –