Reputation: 530
I'm developing a phonegap application using jquery mobile. i didnot get the current location. in on device ready onSucess or onError function is not fired. I think so only i didn't get the location. here is my code.
function onDeviceReady() {
var options = { frequency: 3000 };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); }
function onSuccess(position){
var minZoomLevel = 6;
map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: minZoomLevel;
center: new google.maps.LatLng(position.coords.latitude,position.coords.longitude),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(position.coords.latitude,position.coords.longitude),
map: map,
});
}
function onError() {
alert("Sorry,we cann't find your location");
}
Upvotes: 1
Views: 4644
Reputation: 2299
Please use like this(make sure your device location settings is ON );
navigator.geolocation.getCurrentPosition(onSuccess,onError,{enableHighAccuracy:true});
When i use like this it work on IOS but not working on Android device;
{ maximumAge: 3000, timeout: 3000, enableHighAccuracy: true }
Upvotes: 0
Reputation: 11425
Ok this is what works for me:
Make sure your device or emulator has GPS enabled.
Make sure you installed the geolocation plugin (run phonegap local plugin add org.apache.cordova.geolocation
)
Add a timeout and set enableHighAccuracy:
navigator.geolocation.getCurrentPosition(onSuccess, onError, {timeout: 10000, enableHighAccuracy: true});
In certain emulators you need to set enableHighAccuracy to false, so try that if still doesn't work.
If you are using an Android emulator, it doesn’t read GPS values, so we need to send them via command line. We need to start a telnet session in the port that the emulator is running (you can check the port in the emulator window title, the number at the beginning, in my case 5554):
telnet localhost 5554
And then run the command
geo fix -122.4 37.78
If you close the app you need to re-send the geolocation, so if it doesn’t work, just run the geo fix
command just after opening the app, before the timeout event fires.
Upvotes: 0
Reputation: 77
Please try this
var options = { maximumAge: 3000, timeout: 3000, enableHighAccuracy: true };
navigator.geolocation.getCurrentPosition(success, navFail,options);
If you are using android you have to use "enableHighAccuracy: true"
Thanks
Upvotes: 1
Reputation: 3091
I am having the same issue with some apps I am developing. This problem/solution is peculiar to android devices on some recent versions of phonegap. I cannot remember now where I found this but it works for me:
Do not specify onError callback, wrap the call in a try catch and use that to handle the onError scenario
try{
navigator.geolocation.getCurrentPosition(function (position) {
// If the position can be found send the current location to order the deals
doSomething(position.coords.longitude, position.coords.latitude);
});
}
catch(err){ doSomethingWithoutCoordinates(); }
It almost seems like when you give phonegap an error option it takes it without trying to succeed.
DISCLAIMER: I cannot say for certain that the catch will ever be called, this just worked for me and somehow always gives me a value for the coordinates so you might want to test thoroughly before putting this in production.
Upvotes: 1
Reputation: 2701
**Phonegap android google map with geolocation**
**First add file in html page**
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
Add in javascript
$(document).ready(function(){
navigator.geolocation.getCurrentPosition(onSuccess, onError);
});
function onSuccess(position) {
lat = position.coords.latitude;
lng = position.coords.longitude;
var secheltLoc = new google.maps.LatLng(lat, lng);
var myMapOptions = {
zoom: 16
,center: secheltLoc
,mapTypeId: google.maps.MapTypeId.HYBRID
};
var theMap = new google.maps.Map(document.getElementById("map_canvas"), myMapOptions);
var image = "img/map_pin.png"
var marker = new google.maps.Marker({
map: theMap,
draggable: false,
position: new google.maps.LatLng(lat, lng),
visible: true,
icon: image,
title: Title // Title
});
var infowindow = new google.maps.InfoWindow({
content: contentString
});
infowindow.open(theMap,marker);
google.maps.event.addListener(marker, "click", function (e) {
infowindow.open(theMap,marker);
});
}
/** onError CALLBACK RECEIVES A POSITIONERROR OBJECT **/
function onError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED: alert(MSG_PERMISSION_DENIED);
break;
case error.POSITION_UNAVAILABLE: alert(MSG_POSITION_UNAVAILABLE);
break;
case error.TIMEOUT: alert(MSG_TIMEOUT);
break;
default:
alert(MSG_UNKNOWN);
break;
}
}
Upvotes: 0