2015-12-06 45 views
0

如何指出Google地圖中的特定位置?我試圖看過以前的問題,但我沒有找到任何可以「幫助」我的東西。如何指出Google地圖中的特定位置?

的JavaScript

<script src="https://maps.googleapis.com/maps/api/js"></script> 
<script> 
    function initialize() { 
    var mapCanvas = document.getElementById('map'); 
    var mapOptions = { 
     center: new google.maps.LatLng(58.393584, 15.566656), 
     zoom: 17, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(mapCanvas, mapOptions) 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

CSS

#map {  
width: 500px; 
height: 400px; 
float: left; 
margin: 10px; 
margin-top: 15px; 
} 

(忽略marginfloat),它放大到位置,但我怎麼可以添加一個指向它?指針的圖像會做什麼?可能是一個新手問題,但是如果你能幫助我的話,謝謝。

+0

谷歌[圖](https://developers.google.com/maps/documentation/javascript/examples/marker-simple )[文檔](https://developers.google.com/maps/documentation/javascript/)(向下滾動並點擊標記) –

+0

@JaromandaX我應該只將標記代碼放入我的腳本中,或者放在哪裏? –

+0

是的,我個人將JavaScript代碼放在腳本中 –

回答

2

您錯過了Marker對象。
對於標籤可以使用maplabel.js

function initialize() { 
 
    var mapCanvas = document.getElementById('map'); 
 
    var mapOptions = { 
 
    center: new google.maps.LatLng(58.393584, 15.566656), 
 
    zoom: 17, 
 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }; 
 
    var map = new google.maps.Map(mapCanvas, mapOptions); 
 

 
    // MARKER: 
 
    var marker = new google.maps.Marker({ 
 
    position: mapOptions.center, 
 
    label: "G", 
 
    map: map 
 
    }); 
 
    
 
    // LABEL: 
 
    var mapLabel = new MapLabel({ 
 
    text: "Go here!", 
 
    position: mapOptions.center, 
 
    map: map, 
 
    fontSize: 22, 
 
    align: 'center' 
 
    }); 
 

 
} 
 
google.maps.event.addDomListener(window, 'load', initialize);
#map {  
 
    width: 500px; 
 
    height: 400px; 
 
    float: left; 
 
    margin: 10px; 
 
    margin-top: 15px; 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<script src="https://google-maps-utility-library-v3.googlecode.com/svn/trunk/maplabel/src/maplabel.js"></script> 
 

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

+0

如何爲標記添加標題? –

+1

@JamesSnowy使用:'label:「A」'如果你只想要一個字符,否則這可能有所幫助:https://github.com/googlemaps/js-map-label –

0

而不是使用谷歌地圖API是這樣的,我會親自使用聚合物像谷歌地圖等谷歌

元素

聚合物是所有關於網絡組件,它非常簡單,清潔&易於使用。

如何在谷歌地圖API將與聚合物元件

<google-map latitude="37.77493" longitude="-122.41942" fit-to-markers> 
    <google-map-marker latitude="37.779" longitude="-122.3892" 
     draggable="true" title="This is a title"></google-map-marker> 
    <google-map-marker latitude="37.777" longitude="-122.38911"></google-map-marker> 
</google-map> 

這是非常簡單和靈活的工作實例。

這裏的鏈接,如果你有興趣的文檔:

https://elements.polymer-project.org/elements/google-map

相關問題