简体   繁体   中英

GOOGLE MAPS V2 - Get Current User Position

i'm trying to get user position, so far I'm able to get the 3 stores displayed on the map however i am unable to find my current position, this is done through and emulator, I have implemented the LocationListener method but it seems like the method (onLocationChaged) is never called, in the OnCreate method I have specified my 3 stores which display nicely on the map but not MY CURRENT LOCATION, I don't even have that "raider/round icon" at the top right to take me to my current location, I'm not sure what I'm missing.

Java Code:

public class LocateStore extends FragmentActivity implements LocationListener{

static final LatLng ARCADIA = new LatLng(-25.746318, 28.221322999999984);
static final LatLng HATFIELD = new LatLng(-25.7487333, 28.238043199999993);
static final LatLng CENTURION = new LatLng(-25.8602778, 28.189444399999957);
private GoogleMap map;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_locate_store);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);


    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        Marker aracdia = map.addMarker(new MarkerOptions().position(ARCADIA).title("Arcadia").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_small)));
        Marker hatfield = map.addMarker(new MarkerOptions().position(HATFIELD).title("Hatfield").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_small)));
        Marker centurion = map.addMarker(new MarkerOptions().position(CENTURION).title("Centurion").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_small)));
      }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

         map.clear();

         MarkerOptions mp = new MarkerOptions();

         mp.position(new LatLng(location.getLatitude(), location.getLongitude()));

         mp.title("my position");

         map.addMarker(mp);

         map.animateCamera(CameraUpdateFactory.newLatLngZoom(
         new LatLng(location.getLatitude(), location.getLongitude()), 16));

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }
}

try below code:-

        mapFragment         = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
        map                 = mapFragment.getMap();

        map                 . setMyLocationEnabled(true);

When testing on the emulator (ie without a GPS chip or a network location) you have to enable mock locations. Have this in your manifest:

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

You then need to send the location by using the emulator control view in the DDMS perspective or by Telnet to the emulator, then with the geo fix command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM