admin管理员组文章数量:1429693
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
".dtd">
<html xmlns="" xmlns:v="urn:schemas-microsoft-:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example: Simple Streetview Example</title>
<script src=";amp;v=2.x&key=<?=APIKEY?>"
type="text/javascript"></script>
<script type="text/javascript">
var myPano;
var newPoint;
function initialize() {
var fenwayPark = new GLatLng(42.345573,-71.098326);
var address = "1600 Amphitheatre Parkway, Mountain View, CA, USA";
var geocoder = new GClientGeocoder();
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
newPoint = point;
alert("inside of function: " + newPoint);
}
});
alert("outside of function: " + newPoint);
panoramaOptions = { latlng:fenwayPark };
myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
GEvent.addListener(myPano, "error", handleNoFlash);
}
function handleNoFlash(errorCode) {
if (errorCode == FLASH_UNAVAILABLE) {
alert("Error: Flash doesn't appear to be supported by your browser");
return;
}
}
</script>
when I run this code, the alert("outside of function: " + newPoint); have not got any value, but in the function alert("inside of function: " + newPoint); it gets.
plete code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
".dtd">
<html xmlns="" xmlns:v="urn:schemas-microsoft-:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example: Simple Streetview Example</title>
<script src=";amp;v=2.x&key=ABQIAAAAgVzm2syhpm0A5rAKFMg3FBS-DTtkk0JB-Y_gKL-3MRZwBHch9RSjWJj17-fEEecjCvYeo1i7w_1yPw"
type="text/javascript"></script>
<script type="text/javascript">
var myPano;
function initialize() {
var geocoder = new GClientGeocoder();
var address = "1600 Amphitheatre Parkway, Mountain View, CA, USA";
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
panoramaOptions = { latlng:point };
myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
GEvent.addListener(myPano, "error", handleNoFlash);
}
});
}
function handleNoFlash(errorCode) {
if (errorCode == FLASH_UNAVAILABLE) {
alert("Error: Flash doesn't appear to be supported by your browser");
return;
}
}
</script>
I want to show the street view of address:1600 Amphitheatre Parkway, Mountain View, CA, USA when opening the page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/1999/xhtml" xmlns:v="urn:schemas-microsoft-:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example: Simple Streetview Example</title>
<script src="http://maps.google./maps?file=api&v=2.x&key=<?=APIKEY?>"
type="text/javascript"></script>
<script type="text/javascript">
var myPano;
var newPoint;
function initialize() {
var fenwayPark = new GLatLng(42.345573,-71.098326);
var address = "1600 Amphitheatre Parkway, Mountain View, CA, USA";
var geocoder = new GClientGeocoder();
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
newPoint = point;
alert("inside of function: " + newPoint);
}
});
alert("outside of function: " + newPoint);
panoramaOptions = { latlng:fenwayPark };
myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
GEvent.addListener(myPano, "error", handleNoFlash);
}
function handleNoFlash(errorCode) {
if (errorCode == FLASH_UNAVAILABLE) {
alert("Error: Flash doesn't appear to be supported by your browser");
return;
}
}
</script>
when I run this code, the alert("outside of function: " + newPoint); have not got any value, but in the function alert("inside of function: " + newPoint); it gets.
plete code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/1999/xhtml" xmlns:v="urn:schemas-microsoft-:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example: Simple Streetview Example</title>
<script src="http://maps.google./maps?file=api&v=2.x&key=ABQIAAAAgVzm2syhpm0A5rAKFMg3FBS-DTtkk0JB-Y_gKL-3MRZwBHch9RSjWJj17-fEEecjCvYeo1i7w_1yPw"
type="text/javascript"></script>
<script type="text/javascript">
var myPano;
function initialize() {
var geocoder = new GClientGeocoder();
var address = "1600 Amphitheatre Parkway, Mountain View, CA, USA";
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
panoramaOptions = { latlng:point };
myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
GEvent.addListener(myPano, "error", handleNoFlash);
}
});
}
function handleNoFlash(errorCode) {
if (errorCode == FLASH_UNAVAILABLE) {
alert("Error: Flash doesn't appear to be supported by your browser");
return;
}
}
</script>
I want to show the street view of address:1600 Amphitheatre Parkway, Mountain View, CA, USA when opening the page
Share Improve this question edited Jul 16, 2009 at 4:11 skargor asked Jul 16, 2009 at 0:28 skargorskargor 1,0313 gold badges15 silver badges21 bronze badges4 Answers
Reset to default 4The inner function is a callback function. Meaning, it waits until the geocode api call has finished running before it executes. This is known as asynchronous execution. You'll notice that the outside alert fires before the inside alert even though the inside alert is written earlier.
Any code that needs a valid newPoint value will need to be executed from within the call back function, just like the inside alert.
getLatLng()
is asynchronous.
You call getLatLng()
and some time later it calls your callback with the data. Your "outside of function" code is called immediately after your call to getLatLng()
, before the asynchronous call to your "inside of function" callback happens.
You fixed the problem with the asynchronous getLatLng()
. The only problem with your new code is that there is no panorama at the address you requested (take the '1600' out and it will work). There is a function that will return the point of the nearest panorama: getNearestPanoramaLatLng(). It will return null if there isn't a nearby panorama. Here's how to use it in your code:
function initialize() {
var geocoder = new GClientGeocoder();
var panoClient = new GStreetviewClient();
var address = "1600 Amphitheatre Parkway, Mountain View, CA, USA";
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
panoClient.getNearestPanoramaLatLng(point, function(newPoint) {
if (newPoint == null) {
alert("no panorama found");
return;
}
panoramaOptions = { latlng:newPoint};
myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
GEvent.addListener(myPano, "error", handleNoFlash);
});
}
});
}
Because the callback function where you call the inside alert runs AFTER your outside alert.
Try delaying your outside alert by a second and you'll see that the correct value will be alerted
setTimeout(function(){ alert(newPoint); }, 1000);
This happens because the function where you call the inside alert is asynchronous
EDIT: Please note that the above is bad practice and should only be used to help you understand whats happening and why.
本文标签: google mapsjavascript global variable questionStack Overflow
版权声明:本文标题:google maps - javascript global variable question - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745445390a2658641.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论