1

I am trying to make an app that will show my current location and will track me from there with a line.I have been using Google maps Api v2 for android so i was trying to work with polylines to help me show my tracks but its not showing. Can anyone help me with that..Thankyou. Total code is provided.

public class MainActivity extends FragmentActivity implements ConnectionCallbacks,
    OnConnectionFailedListener, LocationListener,
    OnMyLocationButtonClickListener, OnClickListener, android.location.LocationListener {

private GoogleMap mMap;

private LocationClient mLocationClient;
private TextView mMessageView;
private boolean setIt;


// These settings are the same as the settings for the map. They will in fact give you updates
// at the maximal rates currently possible.
private static final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(5000)         // 5 seconds
        .setFastestInterval(16)    // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_location_demo);
    mMessageView = (TextView) findViewById(R.id.message_text);
    Button b1=(Button)findViewById(R.id.start);
    Button b2=(Button)findViewById(R.id.stop);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
}


@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
    setUpLocationClientIfNeeded();
    mLocationClient.connect();

}

@Override
public void onPause() {
    super.onPause();
    if (mLocationClient != null) {
        mLocationClient.disconnect();
    }
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            mMap.setMyLocationEnabled(true);
            mMap.setOnMyLocationButtonClickListener(this);
        }
    }
}

private void setUpLocationClientIfNeeded() {
    if (mLocationClient == null) {
        mLocationClient = new LocationClient(
                getApplicationContext(),
                this,  // ConnectionCallbacks
                this); // OnConnectionFailedListener
    }
}

/**
 * Button to get current Location. This demonstrates how to get the current Location as required
 * without needing to register a LocationListener.
 */
public void showMyLocation(View view) {
    if (mLocationClient != null && mLocationClient.isConnected()) {
        String msg = "Location = " + mLocationClient.getLastLocation();
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);


    locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        if (v.getId() == R.id.start) {
            setIt = true;
            };
        if (v.getId() == R.id.stop) {
            mMap.clear();
            };


}

 PolylineOptions rectOptions = new PolylineOptions().width(3).color(
        Color.RED);

@Override
public void onLocationChanged(Location location) {
    mMessageView.setText("Location = " + location);


    rectOptions.add(new LatLng(location.getLatitude(), location.getLongitude()));

     if (setIt == true){
          mMap.addPolyline(rectOptions);
     }
}



@Override
public void onConnected(Bundle connectionHint) {
    mLocationClient.requestLocationUpdates(
            REQUEST,
            this);  // LocationListener
}

/**
 * Callback called when disconnected from GCore. Implementation of {@link ConnectionCallbacks}.
 */
@Override
public void onDisconnected() {
    // Do nothing
}

/**
 * Implementation of {@link OnConnectionFailedListener}.
 */
@Override
public void onConnectionFailed(ConnectionResult result) {
    // Do nothing
}

@Override
public boolean onMyLocationButtonClick() {

    {
        setIt = true;
        };
    Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
    // Return false so that we don't consume the event and the default behavior still occurs
    // (the camera animates to the user's current position).
    return false;


}

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

}

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

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}
}
praveen
  • 420
  • 4
  • 18
user3160651
  • 51
  • 1
  • 5

1 Answers1

1

This is very easy to in Google Map API V2 . If you would like to do this then you can follow this reference link:

Go this stackoverflow link

This man already give useful solution. I have done the same way as this site told.

For your facility I have written the main things:

1.create a list of LatLng points such as:

List<LatLng> routePoints;

2.Add the route points to the list (could/should be done in a loop):

routePoints.add(mapPoint);

3.Create a Polyline and feed it the list of LatLng points as such:

Polyline route = map.addPolyline(new PolylineOptions()
  .width(_strokeWidth)
  .color(_pathColor)
  .geodesic(true)
  .zIndex(z));
route.setPoints(routePoints);

Try this and give feedback!!!

Community
  • 1
  • 1
Satyaki Mukherjee
  • 2,857
  • 1
  • 22
  • 26