문제

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. 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.

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

you can use this project to help you get your location, then you can add a marker on that place.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top