Hi,
I have the following code in my location activity.
(this code was copied from Xamarin's Location Services demo)

Collapse | Copy Code

namespace LocationTutorialModified
{
[Activity (Label = "LocationActivity", MainLauncher = true)]
public class LocationActivity : Activity, ILocationListener
{
LocationManager _locMgr; protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);   SetContentView (Resource.Layout.LocationView); // use location service directly
_locMgr = GetSystemService (Context.LocationService) as LocationManager;   _locMgr.RequestLocationUpdates("gps", 0, 0, this);   Location lastKnownLocation = _locMgr.GetLastKnownLocation("gps");     var locationText = FindViewById<TextView>(Resource.Id.locationTextView);   if (lastKnownLocation != null)
{
locationText.Text = string.Format("Lat : {0}, Lon : {1}", lastKnownLocation.Latitude,
lastKnownLocation.Longitude);
}
else
{
locationText.Text = "Unaware of current location";
}
} protected override void OnResume ()
{
base.OnResume (); var locationCriteria = new Criteria (); locationCriteria.Accuracy = Accuracy.NoRequirement;
locationCriteria.PowerRequirement = Power.NoRequirement; string locationProvider = _locMgr.GetBestProvider (locationCriteria, true); _locMgr.RequestLocationUpdates (locationProvider, 0, 0, this);
//_locMgr.RequestLocationUpdates (LocationManager.GpsProvider, 2000, 1, this);
} protected override void OnPause ()
{
base.OnPause (); _locMgr.RemoveUpdates (this);
}   #region ILocationListener implementation
public void OnLocationChanged (Location location)
{
var locationText = FindViewById<TextView> (Resource.Id.locationTextView); locationText.Text += String.Format ("Latitude = {0}, Longitude = {1}", location.Latitude, location.Longitude); //// demo geocoder //new Thread (new ThreadStart (() => {
// var geocdr = new Geocoder (this); // var addresses = geocdr.GetFromLocation (location.Latitude, location.Longitude, 5); // //var addresses = geocdr.GetFromLocationName("Harvard University", 5); // RunOnUiThread (() => {
// var addrText = FindViewById<TextView> (Resource.Id.addressTextView); // addresses.ToList ().ForEach ((addr) => {
// addrText.Append (addr.ToString () + "\r\n\r\n");
// });
// }); //})).Start ();
}   public void OnProviderDisabled (string provider)
{ }   public void OnProviderEnabled (string provider)
{ }   public void OnStatusChanged (string provider, Availability status, Bundle extras)
{ }
#endregion   }
}  

Configuration Options:
1. I have set the project properties --> Target api to 10 since i'm running android 2.3.3 Virtual device from AVD manager
2. I have set project properties for --> Access_Fine / Coarse / Mock locations. They have been put in AndroidManifest after project build
When i deploy the applicaion, I get the "Unaware of current location" (set properly since lets say when the emulator starts, it doesnt know the location).
I'm trying to send DDMS - Mock latitude and longitude by using Emulator Control tab in DDMS tool, Still not getting the locations updated.
Can anyone please see and tell me what's going wrong. Please suggest any modifications and let me try it.
Thanks.

Posted 25-May-12 5:38am

robroysd661

Add a Solution

2 solutions

Sign Up to vote

Solution 1

Working Solution:
Main Issue: The API 10 Device i was running (Android 2.3.3) was not correct.
Although it runs properly, it will not receive DDMS mock locations.
Solution: Change API (Android Virtual Device) to : Google Inc Device, API Level 10 (for Android 2.3.3).
Now the DDMS will send locations properly and Google Inc Api Device, will catch locations and display it. Also, the OnStatusChanged method needs toast message. If not, then i'm getting run time crashes (will find out why).
P.S. I know i'm answering my own question, but this was frustrating to deal with. Hence might help someone.
Thank you Greg Shackles for your solutions.

Collapse | Copy Code

namespace LocationDemo
{
[Activity(Label = "LocationDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class LocationActivity : Activity, ILocationListener
{
private LocationManager _locationManager;   protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);   SetContentView(Resource.Layout.Main);   var welcomeText = FindViewById<textview>(Resource.Id.welcomeMessage);   _locationManager = (LocationManager)GetSystemService(LocationService);
}   protected override void OnResume()
{
base.OnResume();   var criteria = new Criteria();
criteria.PowerRequirement = Power.NoRequirement;
criteria.Accuracy = Accuracy.NoRequirement;   string bestProvider = _locationManager.GetBestProvider(criteria, true);   _locationManager.RequestLocationUpdates(bestProvider, 5000, 20, this);
}   protected override void OnPause()
{
base.OnPause();   _locationManager.RemoveUpdates(this);
}   public void OnLocationChanged(Location location)
{
//var currentLocation = new GeoPoint((int) (location.Latitude * 1e6), (int) (location.Longitude * 1e6)); //_mapOverlay.Add(currentLocation, "Current Location"); //_map.Controller.AnimateTo(currentLocation); var locationText = FindViewById<textview>(Resource.Id.welcomeMessage);   locationText.Text = string.Format("Latitude : {0}, Longitude : {1}", location.Latitude,
location.Longitude);
}   public void OnProviderDisabled(string provider)
{
// called when a provider is disabled
}   public void OnProviderEnabled(string provider)
{
// called when a provider is enabled
}   public void OnStatusChanged(string provider, Availability status, Bundle extras)
{
Toast
.MakeText(this, "Status for " + provider + " changed to " + status, ToastLength.Short)
.Show();
}
}
}  

Permalink

Posted 27-May-12 8:07am

robroysd661

Edited 27-May-12 8:12am

v3

Comments

marcelo heber kuhl - 18-Feb-13 15:48pm

I could not retrieve the location with this code. Can you please post the entire code. I dont understand, where you called the event button to locate?

robroysd - 18-Feb-13 16:30pm

Hey, i'm sorry about this, but currently I have uninstalled monoForAndroid as I've recently upgraded to VS 2012. Still haven't downloaded MONO yet, and will take some time. But as far as I remember, you don't have a button to push in order to get location. During developmet, if you are using the right emulator, it can sent the locations to your phone. _locationManager.RequestLocationUpdates(bestProvider, 5000, 20, this); The above line of code, catches that location code automatically, and geocodes it. Now, let's say you have a Mono for android license, and you are using your android phone to do the debugging. Then, turn the GPS in you phone ON. Make sure you have requested "ACCESS MOCK LOCATION" permission. With the above things checked, all you need to do is, move the phone in your had like you are waving (or something) and the location is updated. So to answer your question, there is no button. During Dev, DDMS (the tool used to send fake locations to your phone) sends the location to your phone. Your phone thinks that It is moving and will geocode the fake location sent by DDMS. During live debug on your phone, you actually move your phone a feet or two for the GPS to kick in, this will get your actual location, where you are sitting and geocode that. By the way: _locationManager.RequestLocationUpdates(bestProvider, 5000, 20, this); in the above line of code, make sure you use 1,1 or 0,0 instead of 5000,20. Someone in a post suggested this for development, debug because 0,0 says catch the location immediately, don't wait for the phone to move 20 feet. I think the 5000 and 20 above suggest how far the phone should move, before location updates are triggered gain. So you making this 0,0 or 1,1 would say.. don't wait at all, catch update without the phone moving, and wait for the phone to move 1 feet, 1 feet respectively. Hope this helps, and sorry I couldn't furnish the code (project doesn't open without an installation of mono for android).

marcelo heber kuhl - 19-Feb-13 11:54am

great robroysd, My code is the same solution 1, but could not make it work. I'm using the emulator, the ANDROID 2.3.3 ... added support for GPS, but could not. I have to move somehow to test? How do I start the application as soon as you give me the location? thanks for everything, I'm learning a lot.

Sign Up to vote

Solution 2

The code above, with some modifications should work. But as the solution I have posted to my original post suggests, the code was ok, but the "emulator" was the problem.
First I sued Android 2.3 this does not work for some reason. Then I installed Google API's for version 2.3, that is.. API level 10. When you start the emulator, don't use the one that says Android 2.3 API level 10, use the new GOOGLE emulator (again with version 2.3 and API 10).
For Google devices, lets say if I'm installing Android 2.3 from the SDK manager.
To get the Google emulator for that, install the sub category: Android 2.3.3 --> Google API's also. This will give you the option to create 2 different API level 10 emulators.
1. The original Android 2.3 API level 10 (which did not work for me)
2. The Google API Android 2.3 level 10 (which actually works by catching locations sent by DDMS, and also works while debugging on an actual android phone).
For simulations with the phone, I use GOOGLE API device as well.
Hope this helps. I will post a code update as soon as I've installed Mono for Android on my VS 2012 copy.
And It's great that you're learning. At the time I wrote this post, I pretty much didn't know anything about it. It comes by time and research (as usual).
Happy coding..

Permalink

Posted 19-Feb-13 6:14am

robroysd661

Comments

marcelo heber kuhl - 20-Feb-13 12:46pm

I installed the Google API's for version 2.3 but when I'm in visual studio and I click the f5 button, appears the emulators available, but there appears another emulator with API 10. I'm trying to do but could not because the emulator does not appear anything like this in the emulator and not have the event "OnLocationChanged" and can not see the location. Let me ask something else, I have to have a license to test mono applications on mobile? Thank you for everything

Mono for Android - LocationServices not working的更多相关文章

  1. 【月入41万】Mono For Android中使用百度地图SDK

    借助于Mono For Android技术,.Net开发者也可以使用自己熟悉的C#语言以及.Net来开发Android应用.由于Mono For Android把Android SDK中绝大部分类库都 ...

  2. Android(1)—Mono For Android 环境搭建及破解

    0.前言 最近公司打算开发一款Android平台的简单报表查询软件,因本人之前一直是.NET开发的,和领导商定之后决定采用Mono For Android 进行开发,暂时采用破解版进行开发: 下文是记 ...

  3. Android(4)—Mono For Android 第一个App应用程序

    0.前言 年前就计划着写这篇博客,总结一下自己做的第一个App,却一直被新项目所累,今天抽空把它写完,记录并回顾一下相关知识点,也为刚学习Mono的同学提供佐证->C#也是开发Android的! ...

  4. mono for android 获取手机照片或拍照并裁剪保存

    axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  5. mono for android Json 上传文件

    void button_Click(object sender, EventArgs e) { string Url = "上传地址,服务器端负责接收"; byte[] fbyte ...

  6. mono for android 用ISharedPreferences 进行状态保持 会话保持 应用程序首选项保存

    由于项目需要 要保持用户登录状态 要进行状态保持 用途就好像asp.net的session一样 登录的时候进行保存 ISharedPreferences shared = GetSharedPrefe ...

  7. mono for android 自定义titleBar Actionbar 顶部导航栏 修改 样式 学习

    以前的我是没有做笔记的习惯的,学习了后觉得自己能记住,但是最近发现很多学的东西都忘记了,所有现在一有新的知识,就记下来吧. 最近又做一个mono for android 的项目 这次调整比较大,上次做 ...

  8. mono for android学习过程系列教程(1)

    直接进入主题,关于mono for android的学习,首先配置好环境,如何配置环境,度娘谷歌一大堆,记得使用破解版. 我自己是百度“黑马四期”传智播客的视频,里面有破解版开发环境的软件. 今天直接 ...

  9. mono for android学习过程系列教程(2)

    接着上一讲继续开始写,今天介绍的是安卓的基本组成结构. 在大多数情况下,MONO FOR ANDROID的命名空间和Android的命名空间 是互相映射的.有时候需要大小写,非字母数字字符的用法以及名 ...

随机推荐

  1. CentOS 7下面配置静态IP

    CentOS 7.0系统是一个很新的版本哦,很多朋友都不知道CentOS 7.0系统是怎么去安装配置的哦,因为centos7.0与以前版本是有很大的改进哦. 说明:截止目前CentOS 7.x最新版本 ...

  2. asp.net部署时加密config文件

    1:运行cmd,并定位到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(可以直接运行vs2005的命令提示工具,但是貌似vs2010默认指向的framewo ...

  3. 试题 F: 特别数的和 第十届蓝桥杯

    试题 F: 特别数的和时间限制: 1.0s 内存限制: 512.0MB 本题总分: 15 分[问题描述]小明对数位中含有 2. 0. 1. 9 的数字很感兴趣(不包括前导 0),在 1 到40 中这样 ...

  4. 咏南中间件修正了一处BUG,调用中间件插件会报:非法访问

    咏南中间件修正了一处BUG,调用中间件插件会报:非法访问将以下方法修改成如下的代码即可function TServerMethods1.GetSvrData(const accountNo, defi ...

  5. web项目开发最佳做法

    一个成熟的web项目应该具备以下基础代码或做法 1.前端基础框架: 统一的ajax 通信/表单提交及调用结果弹窗显示 统一的数据验证 统一的数据列表 2.后端基础框架: 统一的异常处理捕获,可针对具体 ...

  6. Ubuntu 12.04 安装最新版本NodeJS

    昨天搭建了一个Windows NodeJS 运行环境,但Windows 运行NodeJS命令行各种别扭,开源包的编译也是各种问题,折磨了我一天一夜,果断换到Linux 平台.. 我选择了Ubuntu ...

  7. 解决AttributeError: 'Ui_MainWindow' object has no attribute 'show'报错

    1.首先使用pyqt designer来设计ui界面,将其保存为"***.ui"文件, 然后进入到pyqt所在的文件目录中,执行cmd中命令,即在当前目录中可以生成相应的**.py ...

  8. C#基础之流程控制语句详解

    C#程序的执行都是一行接一行.自上而下地进行,不遗漏任何代码.为了让程序能按照开发者所设计的流程进行执行,必然需要进行条件判断.循环和跳转等过程,这就需要实现流程控制.C#中的流程控制包含了条件语句. ...

  9. WCF实现进程间管道通信Demo

    一.代码结构: 二.数据实体类: using System; using System.Collections.Generic; using System.Linq; using System.Run ...

  10. 3. Python的种类