man*_*j82 17 javascript google-maps
How do you get Client location using Google Maps API v3? I tried the following code but kept getting the "google.loader.ClientLocation is null or not an object" error. Any ideas why ??
if (google.loader.ClientLocation) {
alert(google.loader.ClientLocation.latitude+" "+google.loader.ClientLocation.longitude);
}
Run Code Online (Sandbox Code Playgroud)
Thank You
tom*_*ian 33
试试这个 :)
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
function initialize() {
var loc = {};
var geocoder = new google.maps.Geocoder();
if(google.loader.ClientLocation) {
loc.lat = google.loader.ClientLocation.latitude;
loc.lng = google.loader.ClientLocation.longitude;
var latlng = new google.maps.LatLng(loc.lat, loc.lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
alert(results[0]['formatted_address']);
};
});
}
}
google.load("maps", "3.x", {other_params: "sensor=false", callback:initialize});
</script>
Run Code Online (Sandbox Code Playgroud)
Tro*_*rse 10
有点晚了,但我有类似的东西,我正忙着建立,这里是获取当前位置的代码 - 一定要使用本地服务器进行测试.
包括CDN的相关脚本:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMap">
Run Code Online (Sandbox Code Playgroud)
<div id="map"></div>
Run Code Online (Sandbox Code Playgroud)
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
Run Code Online (Sandbox Code Playgroud)
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
var infoWindow = new google.maps.InfoWindow({map: map});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
Run Code Online (Sandbox Code Playgroud)
https://jsfiddle.net/ToreanJoel/4ythpy02/
我无法获得上述代码.
谷歌在这里做了很好的解释:http: //code.google.com/apis/maps/documentation/javascript/basics.html#DetectingUserLocation
他们首先使用W3C Geolocation方法,然后为旧浏览器提供Google.gears后备方法.
这个例子在这里:
http://code.google.com/apis/maps/documentation/javascript/examples/map-geolocation.html
归档时间: |
|
查看次数: |
124073 次 |
最近记录: |