2014-04-29 178 views
-1

目前我正在制作一个允许用户从谷歌地图获取坐标的程序。该程序使用鼠标指针获取地点的位置。如果没有预定的地方,它应该得到一个正确的坐标,但是如果有什么东西(火车,餐馆,医院)我不能得到坐标。我如何获得预定义位置的坐标?如何在谷歌地图预定义位置添加标记

谢谢...

回答

1

我认为以下是你想要的。

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <title>Simple markers</title> 
    <style> 
     html, body, #map-canvas { 
     height: 100%; 
     margin: 0px; 
     padding: 0px 
     } 
    </style> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
<script> 
    var map,marker; 
    var infowindow = new google.maps.InfoWindow(); 
    var geocoder = new google.maps.Geocoder(); 
function initialize() { 
    var myLatlng = new google.maps.LatLng(-25.363882,131.044922); 
    var mapOptions = { 
    zoom: 4, 
    center: myLatlng, 
    mapTypeId: 'roadmap' 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    google.maps.event.addListener(map,'click',function(event) { 
    geocode(event.latLng); 
    }); 
} 
function geocode(position) { 
     geocoder.geocode({'latLng': position}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      if (results[1]) { 
       map.setZoom(11); 
       marker = new google.maps.Marker({ 
        position: position, 
        map: map 
       }); 
       infowindow.setContent(results[1].formatted_address); 
       infowindow.open(map, marker); 
      } else { 
      alert('No results found'); 
     } 
    } else { 
     alert('Geocoder failed due to: ' + status); 
    } 
    });  
} 

google.maps.event.addDomListener(window, 'load', initialize); 
    </script> 
    </head> 
    <body> 
    <div id="map-canvas"></div> 
    </body> 
</html> 

Working Demo。你有没有发现任何困难随意提问。

+0

谢谢你的回应。我试过你的代码,然后我将缩放级别改为14.我找到了“乌鲁鲁 - 卡塔曲塔国家公园”,但是当我点击树形图标时,它不会添加标记。我如何在该地点添加标记? – RicoWijaya