2013-12-14 14 views
26

我正在嘗試將我的google mapma v2功能移植到v3。如何修復Uncaught InvalidValueError:setPosition:不是LatLng或LatLngLiteral:屬性lat:不是數字?

但不知何故,我陷入了一個奇怪的錯誤,我無法找到,我做錯了什麼。

Error : Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number %7Bmain,adsense,geometry,zombie%7D.js:25

這裏我的地圖初始化:

var map = new google.maps.Map(document.getElementById("map"),{ 
    zoom:4 
    size:new google.maps.Size(580,440), 
    mapTypeControl: true, 
    mapTypeControlOptions: { 
    style:google.maps.MapTypeControlStyle.VERTICAL_BAR, 
    position: google.maps.ControlPosition.BOTTOM_CENTER, 
    }, 
    panControl: true, 
    panControlOptions: { 
    position: google.maps.ControlPosition.TOP_LEFT, 
    }, 
    zoomControl: true, 
    zoomControlOptions: { 
    style: google.maps.ZoomControlStyle.LARGE, 
    position: google.maps.ControlPosition.LEFT_CENTER, 
    }, 
    scaleConrol: true, 
    scaleControlOptions:{ 
    position: google.maps.ControlPosition.TOP_LEFT , 
    }, 
}); 
map.setCenter(new google.maps.LatLng(49.477643, 9.316406)); 

,並在這裏與我的錯誤的部分:

function drawTraders(map, options) { 
var traders_uri = options.traders_uri || ''; 
if ('' == traders_uri) { 
    return false; 
} 

// get the informations 
$.getJSON(traders_uri, function(data) { 
    // look through the information 
$.each(data.traders, function(i, trader) { 
var icon = options.icon_image_uri; 

var markerIcon = new google.maps.MarkerImage(icon, 
    // This marker is 20 pixels wide by 32 pixels tall. 
    new google.maps.Size(25, 25), 
    // The origin for this image is 0,0. 
    new google.maps.Point(0,0), 
    // The anchor for this image is the base of the flagpole at 0,32. 
    new google.maps.Point(0, 32) 
); 

var html = 'test'; 

/*Information from chromium debugger 
trader: Object 
    geo: Object 
     lat: "49.014821" 
     lon: "10.985072" 

*/ 
var myLatlng = new google.maps.LatLng(trader.geo.lat,trader.geo.lon); 

/*here is the error in new google.maps.Marker()*/ 
var marker = new google.maps.Marker({ 
    position: myLatlng, 
    map: map, 
    icon : markerIcon, 
}); 

var infowindow = new google.maps.InfoWindow({ 
    content: html 
}); 
} 

編輯:drawTraders

drawTraders(map, { 
     traders_uri: "getTraders.php", 
     icon_image_uri: "icon-cms.gif", 
}); 

的號召,我使用這個例子Info Window

我希望有人能幫助我。

編輯:

Fiddle which is not working

Fiddle which works

+0

GSize是V2的事情。 MarkerImage是已棄用的v3構造,取而代之的是匿名的[Icon](https://developers.google.com/maps/documentation/javascript/reference#Icon)。 'trader.geo.lat,trader.geo.lon'的價值是什麼? – geocodezip

+0

對不起,我還沒有看到它,但修復後google.maps.Size它仍然無法正常工作。這些值在註釋中。我不理解downvote,但確定 – demonking

+0

您沒有發佈足夠的代碼來重現此問題。 – geocodezip

回答

82

Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number

意味着你不及格數字到google.maps.LatLng構造。根據您的意見:

/*Information from chromium debugger 
trader: Object 
    geo: Object 
     lat: "49.014821" 
     lon: "10.985072" 

*/ 

trader.geo.lat和trader.geo.lon是字符串,而不是數字。使用parseFloat將它們轉換爲數字:

var myLatlng = new google.maps.LatLng(parseFloat(trader.geo.lat),parseFloat(trader.geo.lon)); 
+12

我已經嘗試了parseFloat,但仍然是相同的錯誤。 – demonking

+0

「49.014821」在編程中仍然是數字(一個數字)。 – Jared

+3

'LatLng()'*實際上接受字符串罰款*,只要它們是格式良好。 (剛剛在LatLng(lat,lng)'之前添加'alert(lat +「」+ lng +「(類型:」+(typeof lat)+「,」+(typeof lng)+「)」)'來驗證它) –

18

我有完全相同的錯誤消息相同的問題。最後的錯誤是,我仍然稱之爲地圖v2 javascript。我不得不更換:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=####################" type="text/javascript"></script> 

<script src="http://maps.googleapis.com/maps/api/js?key=####################&sensor=false" type="text/javascript"></script> 

在此之後,它工作得很好。我花了一段時間;-)

+0

src-Url ist錯誤,你必須使用v = 3,因爲我的主要職位功能是谷歌地圖v3不是v2;) – demonking

+0

你是對的,我扭轉了代碼部分..這是我如何替換腳本導入。謝謝;-) – CodeCountZero

+0

哦,對不起,我的錯誤,當沒有v = 3變量時,默認的就是googlemaps開發版本,所以要注意。 :) – demonking

3

我有同樣的問題,事實是,經緯度和長輸入應該是字符串。只有那時我才管理。

例如:

控制器。

ViewBag.Lat = object.Lat.ToString()。Replace(「,」,「。」);

ViewBag.Lng = object.Lng.ToString()。Replace(「,」,「。」);

查看 - 功能的JavaScript

<script> 
    function initMap() { 
     var myLatLng = { lat: @ViewBag.Lat, lng: @ViewBag.Lng}; 

     // Create a map object and specify the DOM element for display. 
     var map = new window.google.maps.Map(document.getElementById('map'), 
     { 
      center: myLatLng, 
      scrollwheel: false, 
      zoom: 16 
     }); 

     // Create a marker and set its position. 
     var marker = new window.google.maps.Marker({ 
      map: map, 
      position: myLatLng 
      //title: "Blue" 
     }); 
    } 
</script> 

我的double值轉換爲字符串,並做的 '' 一個替換 ''所以一切正常。

0

你可能會傳遞null值,如果你是動態加載的座標,設置檢查調用地圖加載器即前:if(mapCords){loadMap}

0

使用angular-google-maps

$scope.bounds = new google.maps.LatLngBounds(); 
for (var i = $scope.markers.length - 1; i >= 0; i--) { 
    $scope.bounds.extend(new google.maps.LatLng($scope.markers[i].coords.latitude, $scope.markers[i].coords.longitude)); 
};  
$scope.control.getGMap().fitBounds($scope.bounds); 
$scope.control.getGMap().setCenter($scope.bounds.getCenter()); 
相關問題