更新用户的位置 [英] Updating user's location
本文介绍了更新用户的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些麻烦与谷歌地图。我需要在地图上显示用户的位置,然后对其进行更新。我使用的是默认的标记。然而,我的方式code当前设置,它的每一个OnLocationChanged调用时增加了一个新的标记。我希望它只是更新一个标志或删除旧的,因为它增加了一个新的当前位置。这里的code:
公共类GoogleMapActivity扩展FragmentActivity实现LocationListener的{
双longitude_user,latitude_user;
koordinate_user的LatLng;
GoogleMap的supportMap;
字符串名称,地址,类型;
BitmapDescriptor BDF;
私人的LocationManager的LocationManager;
私人字符串供应商;
地点位置;
标记标记;保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_googlemaps); SupportMapFragment FM =(SupportMapFragment)getSupportFragmentManager()findFragmentById(R.id.map)。
supportMap = fm.getMap(); 的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE); 标准标准=新标准();
供应商= locationManager.GPS_PROVIDER;
如果(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
供应商= locationManager.NETWORK_PROVIDER;
}
如果(供应商== NULL){
供应商= locationManager.getBestProvider(标准,FALSE);
} 如果(位置!= NULL){
的System.out.println(提供者+提供商+已被选择。);
onLocationChanged(位置);
}其他{
Log.i(测试,位置不可用);
} 位置= locationManager.getLastKnownLocation(供应商);}/ *请求更新在启动* /
@覆盖
保护无效onResume(){
super.onResume();
locationManager.requestLocationUpdates(供应商,1000,10,本);
//Log.i(\"test,提供者:+提供商);
} / *删除LocationListener的更新时,活动暂停* /
@覆盖
保护无效的onPause(){
super.onPause();
locationManager.removeUpdates(本);
} @覆盖
公共无效onLocationChanged(地点){
latitude_user = location.getLatitude();
longitude_user = location.getLongitude(); 如果(latitude_user = NULL&放大器;!&安培;!longitude_user = NULL){
koordinate_user =新的经纬度(latitude_user,longitude_user);
标记= supportMap.addMarker(新的MarkerOptions()
.POSITION(koordinate_user));
} } @覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
// TODO自动生成方法存根 } @覆盖
公共无效onProviderEnabled(字符串提供商){
Toast.makeText(这一点,已启用新的供应商+供应商,
Toast.LENGTH_SHORT).show(); } @覆盖
公共无效onProviderDisabled(字符串提供商){
Toast.makeText(这一点,残疾人提供+供应商,
Toast.LENGTH_SHORT).show();
}
}
解决方案
更新此你的 onLocationChanged
法
@覆盖
公共无效onLocationChanged(地点){
latitude_user = location.getLatitude();
longitude_user = location.getLongitude();如果(latitude_user = NULL&放大器;!&安培;!longitude_user = NULL){
如果(标记!= NULL)
marker.remove();
koordinate_user =新的经纬度(latitude_user,longitude_user);
标记= supportMap.addMarker(新的MarkerOptions()
.POSITION(koordinate_user));
}}
希望这是你想要的。
I'm having some trouble with Google Maps. I need to display user's location on the map and then update it. I'm using a default marker. However, the way my code is currently set up, it adds a new marker every time OnLocationChanged is called. I want it to just update that one marker or remove old one as it adds a new one with current location. Here's the code:
public class GoogleMapActivity extends FragmentActivity implements LocationListener {
Double longitude_user, latitude_user;
LatLng koordinate_user;
GoogleMap supportMap;
String title, address, type;
BitmapDescriptor bdf;
private LocationManager locationManager;
private String provider;
Location location;
Marker marker;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_googlemaps);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
supportMap = fm.getMap();
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.GPS_PROVIDER;
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
provider = locationManager.NETWORK_PROVIDER;
}
if (provider == null){
provider = locationManager.getBestProvider(criteria, false);
}
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
Log.i("test", "Location not available");
}
location = locationManager.getLastKnownLocation(provider);
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 1000, 10, this);
//Log.i("test", "Provider: " + provider);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
latitude_user = location.getLatitude();
longitude_user = location.getLongitude();
if (latitude_user != null && longitude_user != null) {
koordinate_user = new LatLng(latitude_user, longitude_user);
marker = supportMap.addMarker(new MarkerOptions()
.position(koordinate_user));
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
解决方案
Update your onLocationChanged
method by this
@Override
public void onLocationChanged(Location location) {
latitude_user = location.getLatitude();
longitude_user = location.getLongitude();
if (latitude_user != null && longitude_user != null) {
if(marker != null)
marker.remove();
koordinate_user = new LatLng(latitude_user, longitude_user);
marker = supportMap.addMarker(new MarkerOptions()
.position(koordinate_user));
}
}
Hope this is what you wanted.
这篇关于更新用户的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文