2013-12-13 99 views
-1

上的標誌我建立這個腳本初始化一個地圖的特定點:顯示谷歌地圖地圖

注:(x, y)的經緯度座標。

<script type="text/javascript"> 
     var businessMap; 

     function showBusinessMap(x, y) { 
      var xops = 
      { 
       zoom: 15, 
       center: new google.maps.LatLng(x, y) 
      } 

      var chosenPoint = new google.maps.Marker({ 
       position: new google.maps.LatLng(x, y), 
      }); 

      businessMap = new google.maps.Map(document.getElementById("businessMap"), xops); 

      chosenPoint.setMap(businessMap); 
      chosenPoint.setVisible(true); 


     } 
</script> 

它工作的很好,但沒有標記顯示在地圖上。

我也有這個jQuery的功能調整大小的地圖:

要顯示
  $('#showMap').on('shown', function() { 
       google.maps.event.trigger(businessMap, "resize"); 
      }); 

我怎樣才能使標記chosenPoint

如果我錯過了任何信息,請讓我知道,我會添加它。

在此先感謝!

編輯:

var businessMap; 

    function showBusinessMap(x, y) { 
     var xops = 
     { 
      zoom: 15, 
      center: new google.maps.LatLng(x, y) 
     } 

     businessMap = new google.maps.Map(document.getElementById("businessMap"), xops); 

     var chosenPoint = new google.maps.Marker({ 
      position: new google.maps.LatLng(x, y), 
      map: businessMap 
     }); 
    } 

還不行。

+0

定義「不起作用」。我懷疑它工作得很好,你的地圖div沒有大小(至少它適用於本地副本)。 – geocodezip

+0

http://www.geocodezip.com/v3_SO_simpleMap_businessMap.html – geocodezip

回答

0

編輯: 您需要將您創建的chosenPoint添加到地圖。

從谷歌地圖API,

var myLatlng = new google.maps.LatLng(-25.363882,131.044922); 
var mapOptions = { 
    zoom: 4, 
    center: myLatlng 
} 
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

// To add the marker to the map, use the 'map' property 
var marker = new google.maps.Marker({ 
    position: myLatlng, 
    map: map, 
    title:"Hello World!" 
}); 

所以,你的情況,首先要創建地圖對象,則該標記對象第二,和上面包括map: businessMap爲標記的選項,就像例子。

Look here for more information.

0
var address = "Address, City, ProvinceOrState, Country"; 

geocoder.geocode({ 'address': address }, function (results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     //console.log("GeocoderStatus.OK : " + results[0].geometry.location); 
     var mapOptions = { 
      zoom: 8, 
      center: results[0].geometry.location, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     } 
     //load the map 
     map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 

    } //if 
    else { console.log("Geocode was not successful for the following reason: " + status); } 
}) 

以下部分是添加的「標記」

  var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
0

作爲一個起點,這裏是你的代碼工作的簡單示範基地的一部分。我想你的問題是由你向我們展示的東西以外的東西引起的。

<script type="text/javascript"> 

     var businessMap; 

     function showBusinessMap(x, y) { 

      var xops = { 
       zoom: 15, 
       center: new google.maps.LatLng(x, y) 
      }; 
      var businessMap = new google.maps.Map(document.getElementById("businessMap"), 
      xops); 

      var chosenPoint = new google.maps.Marker({ 
       position: new google.maps.LatLng(x, y), 
       map: businessMap 
      }); 

     } 

     function initialize() { 
      showBusinessMap(-34.397, 150.644); 
     } 

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

</script>