LocationManagerAndroid上的API似乎对于仅需要偶尔粗略估计用户位置的应用程序来说有点痛苦.
我正在处理的应用程序本身并不是一个位置应用程序,但它确实需要获取用户的位置才能显示附近的商家列表.它不需要担心用户是否在移动或类似的东西.
这是我想做的事情:
ActivityX中需要它时,它将可用.看起来它应该不那么难,但在我看来,我必须启动两个不同的位置提供商(GPS和NETWORK)并管理每个生命周期.不仅如此,我还必须在多个活动中复制相同的代码以满足#2.我曾尝试过使用getBestProvider()过去将解决方案缩减为只使用一个位置提供商,但这似乎只能为您提供最好的"理论"提供商,而不是实际上会给您带来最佳结果的提供商.
有没有更简单的方法来实现这一目标?
我在使用Android定位系统的NETWORK提供商获取当前位置坐标时遇到了麻烦.
已经阅读了很多教程,并为我的项目实现了4或5个现有的类,所有这些都给了我最后的坐标但不是当前的坐标.
我很确定问题是我缺少的基本问题,但我无法理解究竟是什么.
我现在使用的代码:
这是我的主要活动
package com.example.locationtests;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GPSTracker mGPS = new GPSTracker(this);
TextView text = (TextView) findViewById(R.id.texts);
if(mGPS.canGetLocation ){
mGPS.getLocation();
text.setText("Lat"+mGPS.getLatitude()+"Lon"+mGPS.getLongitude());
}else{
text.setText("Unabletofind");
System.out.println("Unable");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用于跟踪的课程:
package com.example.locationtests;
import android.app.AlertDialog; …Run Code Online (Sandbox Code Playgroud)