1

Just got familiar with the Google Maps API (v2) and integrated it into my android app and i have 2 questions that i didn't find what is the best way to do it:

The first this is always focus on current user location (like GPS application). What i did is in the onCreate i get the user location (according to some stuff i found here and there))

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

        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        LatLng currentUserPosition = new LatLng(location.getLatitude(), location.getLongitude());
        // setting the cam position to current position
        CameraPosition camPosition = new CameraPosition(currentUserPosition, 17f, 30f, 0f);
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(camPosition);
        mapFragment.getMap().animateCamera(update);

        // updating map every second
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                1000, // 1-second interval.
                10, // 10 meters.
                listener);
}

Where listener is a LocationListener configured like this:

private final LocationListener listener = new LocationListener() {

    @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

    }

    @Override
    public void onLocationChanged(Location location) {
        CameraPosition camPosition = new CameraPosition(new LatLng(location.getLatitude(), location.getLongitude()), 17f, 30f, 0f);
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        CameraUpdate update = CameraUpdateFactory.newCameraPosition(camPosition);
        mapFragment.getMap().animateCamera(update);
    }
};

Is this the correct way to do so?
The second thing is that the user can touch the map and dynamically add markers on the map (with his own titles and stuff). I tried to find (but didn't) a way to draw a path between the markers and act like a gps application. Is this even possible?
Thank you

Mr T.
  • 4,278
  • 9
  • 44
  • 61

2 Answers2

1

I strongly suggest following the Developer Guide tutorial from the documentation for the proper way to integrate Maps Android API v2 into your app, and also checking the sample code bundled with the Google Play services SDK.

The best (and easiest) way to automatically follow user's (My Location dot) location is implementing OnMyLocationChangeListener and updating the camera accordingly in the callback provided by that interface.

For creating a series of line segments that connect the markers you should be able to use the Polyline class. Edit: You may find this related answer useful for drawing a path between two markers.

Community
  • 1
  • 1
Nevermore
  • 995
  • 1
  • 9
  • 11
0

Well actually in Google Map API V2 you can use the:

map.setMyLocationEnabled(true);

this will create an icon on the Google Map window to follow user current location. Actually I think it's easier then implement the whole LocationListener.

about the Markers question you will need to implement some kind of window where user can set all the marker data. and then you can add the marker dynamically like this:

marker = map.addMarker(new MarkerOptions().position(latlng).title(markersTitleArray[i]).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));

if you want to add this marker on click of the map then you will need to implement:

map.setOnMapClickListener(this);
map.setOnMapLongClickListener(this);

in your Activity.

Finally about your last question as you already been provided with my answer on how to draw Polyline on the map for directions:

Draw driving route between 2 GeoPoints on GoogleMap SupportMapFragment

Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187