I am able to get my own Longitude and Latitude and save them to vars:
var myLat = position.coords.latitude;
var myLong = position.coords.longitude;
I'm just not sure how to pass them to the code that brings up the intial position of map on the screen:
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
I need to feed myLat and myLong into the line:
center: new google.maps.LatLng(-34.397, 150.644)
Im just not sure how to go about it. Can anyone point me in the right direction?
EDIT: Added full code as per request below
<script type="text/javascript" src=" KEY HERE&sensor=true"></script>
<script type="text/javascript">
//MAP
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 9,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
//GEOLOCATION
var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n');
var myLat = position.coords.latitude;
var myLong = position.coords.longitude;
map.setCenter(new google.maps.LatLng(myLat, myLong))
};
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
</script>
And HTML:
<body onload="initialize()">
<div id="map_canvas" style="width:300px; height:400px;"></div>
</body
>
I am able to get my own Longitude and Latitude and save them to vars:
var myLat = position.coords.latitude;
var myLong = position.coords.longitude;
I'm just not sure how to pass them to the code that brings up the intial position of map on the screen:
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
I need to feed myLat and myLong into the line:
center: new google.maps.LatLng(-34.397, 150.644)
Im just not sure how to go about it. Can anyone point me in the right direction?
EDIT: Added full code as per request below
<script type="text/javascript" src="http://maps.googleapis./maps/api/js?key=YOUR KEY HERE&sensor=true"></script>
<script type="text/javascript">
//MAP
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 9,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
//GEOLOCATION
var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n');
var myLat = position.coords.latitude;
var myLong = position.coords.longitude;
map.setCenter(new google.maps.LatLng(myLat, myLong))
};
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
</script>
And HTML:
<body onload="initialize()">
<div id="map_canvas" style="width:300px; height:400px;"></div>
</body
>
Share Improve this question edited Aug 6, 2012 at 10:29 MeltingDog asked Aug 1, 2012 at 5:00 MeltingDogMeltingDog 15.6k52 gold badges178 silver badges322 bronze badges 1-
Why can't you just do
new google.maps.LatLng(myLat, myLng)
? – Amit Kumar Gupta Commented Aug 1, 2012 at 5:09
3 Answers
Reset to default 3The reason why this is not working is beacuse of the following function
var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n');
var myLat = position.coords.latitude;
var myLong = position.coords.longitude;
map.setCenter(new LatLng(myLat, myLong));
};
When you use
map.setCenter(new LatLng(myLat, myLong))
you have to use
map.setCenter(new google.maps.LatLng(myLat, myLong))
The LatLng object will be undefined. The Object you need to use is google.maps.LatLng.
It's just
var map;
function initialize() {
//...
}
initialize();
function initiate_geolocation() {
navigator.geolocation.getCurrentPosition(onSuccess);
}
function onSuccess(position){
var myLat = position.coords.latitude;
var myLong = position.coords.longitude;
map.setCenter(new google.maps.LatLng(myLat, myLong)); // edit this line
}
initiate_geolocation();
It's not map.setCenter(new LatLng(myLat, myLong));
instead it should bemap.setCenter(new google.maps.LatLng(myLat, myLong));
DEMO.
You can simply pass myLat and myLong as constructor argument in google.maps.LatLng. So your code should look like :
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(myLat,myLong),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745358813a4624265.html
评论列表(0条)