[英]How to track and display user's location on a map
The title pretty much covers it. 标题几乎涵盖了它。 I want to use the phone's GPS to track the user's location and display a marker on the map at their location.
我想使用手机的GPS来跟踪用户的位置,并在他们的位置在地图上显示标记。 I want this marker to be almost constantly updating its location so that if the user moves the marker will shift as well.
我希望此标记几乎可以不断更新其位置,以便用户移动标记时也会随之移动。
I can display the map fine, but I am having trouble figuring out how to use the location services. 我可以很好地显示地图,但是在弄清楚如何使用位置服务时遇到了麻烦。 The code I am using to display the map is below.
我用来显示地图的代码如下。
Layout: 布局:
<?xml version="1.0" encoding="utf-8"?>
<!-- The layout for the MainMap class -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
Map Activity: 地图活动:
public class MainMap extends FragmentActivity {
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_map);
// Get a handle to the Map Fragment
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (map != null) {
// The Map is verified. It is now safe to manipulate the map.
}
}
}
}//MainMap
Any help is appreciated. 任何帮助表示赞赏。 Thank you.
谢谢。
You need to add LocationListener for this. 您需要为此添加LocationListener。
public class MainMap extends FragmentActivity{
public LocationManager locationManager;
public LocationUpdateListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_map);
// Get a handle to the Map Fragment
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new LocationUpdateListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (map != null) {
// The Map is verified. It is now safe to manipulate the map.
}
}
}
class LocationUpdateListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
// update your marker here
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
http://www.javacodegeeks.com/2010/09/android-location-based-services.html http://www.javacodegeeks.com/2010/09/android-location-based-services.html
you can use this project to help you get your location, then you can add a marker on that place. 您可以使用此项目来帮助您获取位置,然后可以在该位置添加标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.