2016-07-29 64 views
0

我是谷歌地圖中的新成員。 我使用此代碼來顯示谷歌地圖。我怎樣才能在谷歌地圖中獲得並標記一個位置

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://maps.googleapis.com/maps/api/js"></script> 
<script> 
function initialize() { 
    var mapProp = { 
    center:new google.maps.LatLng(32.39793603710494,51.39232635498047), 
    zoom:5, 
    mapTypeId:google.maps.MapTypeId.ROADMAP 
    }; 
    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp); 
} 
google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
</head> 

<body> 
<div id="googleMap" style="width:500px;height:380px;"></div> 
</body> 

</html> 

現在我要標記並獲得緯度和經度,當我點擊它的地方,我想經度和緯度保存在database.How我能做到這一點?

+0

可能重複(http://stackoverflow.com/questions/27307110/cant-add-markers-to-custom-tile -map) – geocodezip

+0

可能的重複[如何創建自定義,共享和協作映射?](http://stackoverflow.com/questions/19015842/how-can-i-create-a-custom-shareable-and-合作地圖) – geocodezip

+0

請參閱[使用帶Google地圖的PHP/MySQL](https://developers.google.com/maps/articles/phpsqlajax_v3) – geocodezip

回答

2

我已經把我的答案,因爲我沒有看到任何相關的答案在兩個尖銳的問題。

var map = new google.maps.Map(document.getElementById('map_canvas'), { 
     zoom: 1, 
     center: new google.maps.LatLng(35.137879, -82.836914), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

var myMarker = new google.maps.Marker({ 
    position: new google.maps.LatLng(47.651968, 9.478485), 
    draggable: true 
}); 

google.maps.event.addListener(myMarker, 'dragend', function (evt) { 
// from here you can get where point marker user put and get that let long using 
// Lat = evt.latLng.lat().toFixed(3) 
// Lng = evt.latLng.lng().toFixed(3) 
// after this you can do ajax call and save at you server and do what ever you wanted to do ... 

    document.getElementById('current').innerHTML = '<p>Marker dropped: Current Lat: ' + evt.latLng.lat().toFixed(3) + ' Current Lng: ' + evt.latLng.lng().toFixed(3) + '</p>'; 
}); 

google.maps.event.addListener(myMarker, 'dragstart', function (evt) { 
    document.getElementById('current').innerHTML = '<p>Currently dragging marker...</p>'; 
}); 

map.setCenter(myMarker.position); 
myMarker.setMap(map); 

html 

<body> 
    <section> 
     <div id='map_canvas'></div> 
     <div id="current">Nothing yet...</div> 
    </section> 
</body> 

工作演示:中[廣東話添加標記到自定義瓷磚地圖] link

相關問題