PDA

View Full Version : Initialize Google map to the current user position



myput
14 Sep 2011, 1:23 AM
Hello everybody,

i try to use google map API for give the user's current position and place it on marker map. But in all example, there is raw positions for latitude and longitude.

Is it not possible to initialize the center of the map on the user current position ??

I give you my code for trying to make better:


myApp.views.ShowCurrentPosMap = Ext.extend(Ext.Panel, {
layout:'card',
title: 'Map',
iconCls: 'maps',
initComponent: function(){
this.items = getMap();
myApp.views.ShowCurrentPosMap.superclass.initComponent.call(this);
}
});


Ext.reg('showCurrentPosMap', myApp.views.ShowCurrentPosMap);



In my Views, i call the function getMap who will create the map etc ...

After, in GeoLocation file, i have some functions:


var DefaultLat = 45.936661;
var DefaultLong = 6.630557;


function initInfoWindow(map) {
var infowindow = new google.maps.InfoWindow({
content: map.description,
});
return infowindow;
}


function setInfoWindow(infowindow, map, marker) {
infowindow.open(map, marker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
}


function setMarker(map, titlemap) {
var latlong = new google.maps.LatLng(DefaultLat, DefaultLong);
var marker = new google.maps.Marker({
position: latlong,
title : titlemap,
map: map
});
return marker;
}


function createNewMapPanel(map, titlepanel, xtypepanel) {
this.panel = new Ext.Panel({
layout: 'fit',
items: map,
dockedItems:[
{
xtype: xtypepanel,
title: titlepanel,
}
]
});
return this.panel;
}


function getMap() {
this.map = new Ext.Map ({
useCurrentLocation: true,
mapOptions : {
zoom: 12,
timeout: 10000,
maximumAge: 20000,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.DEFAULT
},
autoUpdate:true,
center: new google.maps.LatLng(DefaultLat, DefaultLong),
},
listeners: {
maprender : function(comp, map) {
var infowindow = initInfoWindow(map);
var marker = setMarker(map, map.title);
setInfoWindow(infowindow, map, marker);
}
}
});
return createNewMapPanel(this.map, 'My Map', 'toolbar');
}


Sorry for put all the code, i don't really know where is the problem.

I have in raw in the code:


var DefaultLat = 45.936661;
var DefaultLong = 6.630557;
var latlong = new google.maps.LatLng(DefaultLat, DefaultLong);

But i don't want. I want google map API find alone where is my current position and give variables in my code of that.
Is it possible ???



Thanks for your answers :)

Martialen
26 Sep 2011, 7:36 AM
Bonjour mon ami. Take a look at this post and in your getMap() function, where you define a new Ext.Map, copy/paste the geo object from this post:

http://www.sencha.com/forum/showthread.php?121673-Get-current-location-on-map

(http://www.sencha.com/forum/showthread.php?121673-Get-current-location-on-map)When you set useCurrentLocation to true, the Ext.Map object is supposed to automatically center the map on the user's current location. It should detect where you are and provide those coordinates to the Google Map. If your location is not detected, then it centers the map on Mountain View, i.e. Google's headquarters.

If you want it to be centered on your Default Long/Lat instead, then you should modify the geo parameter in the post I linked you to. Where the geo object defines locationerror: you should specify there that the map centers on your default coordinates.