简体   繁体   English

在Android中使用Google Map API V2显示我的足迹

[英]Showing my track with Google map api v2 in android

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. 我正在尝试制作一个可以显示我当前位置并使用line从那里跟踪我的应用程序。我一直在使用适用于Android的Google Maps Api v2,所以我试图使用折线来帮助我显示我的足迹,但是展示。 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

}
}

This is very easy to in Google Map API V2 . 在Google Map API V2中,这非常容易。 If you would like to do this then you can follow this reference link: 如果您想这样做,可以点击以下参考链接:

Go this stackoverflow link 转到此stackoverflow链接

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: 1.创建LatLng点列表,例如:

List<LatLng> routePoints;

2.Add the route points to the list (could/should be done in a loop): 2.将路由点添加到列表中(可以/应该循环执行):

routePoints.add(mapPoint);

3.Create a Polyline and feed it the list of LatLng points as such: 3.创建一条折线,并向其输入LatLng点列表:

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

Try this and give feedback!!! 试试这个并给出反馈!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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