我可以翻译。您想获取当前用户位置的地址字符串,这是否可能?
非常感谢。 问候
非常感谢。 问候
http://code.google.com/apis/ajax/documentation/#ClientLocation
如果您正在尝试获取“街道地址”!
请同时包括JS和MAP Api密钥,从向导中保持所有内容在一起
我的代码看起来像这样:(还使用了一点jQuery)
var geocoder = null;
var lat = null;
var lng = null;
$(function() {
if (GBrowserIsCompatible()) {
if (google.loader.ClientLocation &&
// i used this here cause not always
//this retrieve the correct LAT & LON
// so set it manually
google.loader.ClientLocation.address.country_code == "IT" ) {
lat = google.loader.ClientLocation.latitude;
lng = google.loader.ClientLocation.longitude;
} else {
//alert( 'Insert Your Latitude & longitude manually!' );
lat = '42.464826';
lng = '14.214095';
}
//creat the full LAT + LON
points = new GLatLng( lat , lng );
//get the Street Address
get_address(points);
}
});
function get_address(latlng) {
if (latlng) {
geocoder = new GClientGeocoder();
geocoder.getLocations(latlng, function(addresses) {
if(addresses.Status.code != 200) {
alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
}
else {
address = addresses.Placemark[0];
var myHtml = address.address;
$('#address').text(myHtml);
}
});
}
};
也许阅读这个链接也会有所帮助:
是的,使用HTML5和Google API 3可以实现,您需要更改API密钥并将map-canvas作为HTML正文中的一个元素。
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=yourAPIKey&sensor=false"></script>
<script type="text/javascript">
var lat;
var lng;
function initialize() {
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position)
{
lat = position.coords.latitude;
lng = position.coords.longitude;
var myLatlng = new google.maps.LatLng(lat, lng);
var myTittle = "My Current Location! Lat: " + lat + "lng: " + lng;
var marker = new google.maps.Marker({
position: myLatlng,
title: myTittle
});
var mapOptions = {
center: myLatlng,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="Map" style="width: 100%; height: 600px;"> </div>
<script type="text/javascript">
$(function(){
var zoom = 11;
function success(position) {
var LAT = position.coords.latitude;
var LONG = position.coords.longitude;
$('#Map').append('<p>Current Latitude: ' + LAT + '<br/>Current Logitude: ' + LONG +'<br/><br/>Note* This may not display your exact location, but just your city. Works better on mobile devices</p>');
var map = '<iframe style="width: 100%; height: 600px;" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.ca/maps?source=s_d&saddr=' + LAT +',+' + LONG +'&daddr=**YOUR_DESTINATION_ADDRESS**&ie=UTF8&t=hz=' + zoom + '&output=embed"></iframe>';
$('#Map').append(map);
}
function error(){
/*Error Code*/
}
function MakeMap(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
}
}
MakeMap();
});
</script>