java - 在 android 中使用 Google map api v2 显示我的轨迹

标签 java android google-maps google-maps-api-2

我正在尝试制作一个应用程序来显示我的当前位置,并会用一条线从那里跟踪我。我一直在使用适用于 Android 的 Google map Api v2,所以我尝试使用多段线来帮助我显示我的足迹但它没有显示。 任何人都可以帮助我......谢谢。 提供了全部代码。

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

}
}

最佳答案

这在 Google Map API V2 中非常容易。如果您想这样做,那么您可以点击此引用链接:

Go this stackoverflow link

这个人已经给出了有用的解决方案。我已经按照这个网站告诉的方式做了。

对于你的设施,我写了主要的东西:

1.创建 LatLng 点列表,例如:

List<LatLng> routePoints;

2.将路线点添加到列表中(可以/应该在循环中完成):

routePoints.add(mapPoint);

3. 创建一条多段线并将 LatLng 点的列表提供给它:

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

试试这个并提供反馈!!!

关于java - 在 android 中使用 Google map api v2 显示我的轨迹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20942492/

相关文章:

java - "</>"在 Java 中有什么意义吗?

javascript - 谷歌地图 API : getFeatureById for feature collection not working

ios - 如何在 IOS 中弯曲多段线(谷歌地图)?

java - 为 TeamCity 配置 Java 版本

java - 如何从 Outlook 中的其他组和文件夹而不是收件箱下载或获取附件

java - 将日期字符串转换为不同的格式

java - 如何在Android的PreferenceScreen中使用textview?

android - react-native android bundle 版本无法在某些设备上运行

android - 一旦我按下主页按钮并重新启动 Activity , Activity 如何可见?

google-maps - 使用 Google Maps API v3 禁用兴趣点信息窗口