Android总线跟踪应用程序,用于跟踪地图上的位置和更新位置

时间:2015-02-02 15:24:48

标签: android google-maps location

我一直在尝试创建一个总线跟踪应用程序,但我一直坚持让应用程序定期显示手机的位置。我的目标是找到手机并在地图上绘图。我一直试图让标记显示我当前的位置并每秒更新一次(我使用手机跟踪公交车)

import android.location.Location;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity implements
        OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {
private final String TAG = "ThisIsStressful";

private TextView mLocationView;

private GoogleApiClient mGoogleApiClient;

private LocationRequest mLocationRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLocationView = new TextView(this);

    setContentView(R.layout.activity_main);
    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);


    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

public void onMapReady(GoogleMap map) {


    //enable location of user
    map.setMyLocationEnabled(true);


    //map.moveCamera(CameraUpdateFactory.newLatLngZoom(mapCenter, 163));


    // One example of Bus stop (Hard coded bus stop)
    map.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
            .position(new LatLng(2.975177, 101.729509))
            .title("Murni")
            .snippet("Bus Stop")
            .flat(true));
}


@Override
protected void onStart() {
    super.onStart();
    // Connect the client.
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    // Disconnecting the client invalidates it.
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onConnected(Bundle bundle) {

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000); // Update location every second

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "GoogleApiClient connection has been suspend");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "GoogleApiClient connection has failed");
}

@Override
public void onLocationChanged(Location location) {
    mLocationView.setText("Location received: " + location.toString());
}

正如您在位置更改方法中看到的那样。我可以用字符串来表示。但我不知道如何将它作为标记显示在地图上。

1 个答案:

答案 0 :(得分:0)

尝试在onLocationChanged方法中添加以下代码。

map.addMarker(new MarkerOptions()     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
           .position(new LatLng(location.getLatitude(), location.getLongitude()))
           .title("My Location")
           .snippet("Current Location")
           .flat(true));
相关问题