简体   繁体   English

如何在地图上跟踪和显示用户的位置

[英]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.

相关问题 如何跟踪android应用程序用户的位置 - How to track the android application user's location 如何跟踪用户位置并使用Google Map制作路线 - how to track user location and make the route with google map 如何跟踪Facebook当前用户的位置IOS / Android - How to track facebook current user's location IOS/Android 如何让天气信息默认显示用户当前位置的天气? Openweather map - How can I make the weather information default to display the weather in the user's current location? Openweather map 跟踪用户位置的前台或后台服务? - Foreground or Background service to track user's location? 如何在数据库中发送和接收用户的位置以及如何在地图中使用它[Android] - How to send and receive user's location to database and use it in map [Android] 如何在Google地图中更新用户的当前位置 - How update user's current location in google map Android Maps-如何将地图自动居中到用户的当前位置? - Android Maps - how to automatically center the map to the user's current location? Android:在地图片段上显示用户位置 - Android: Display user location on Map Fragment 在运动过程中如何跟踪手指的位置? - How to track a finger's location during movement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM