Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中使用Google map api v2显示我的曲目_Java_Android_Google Maps_Google Maps Api 2 - Fatal编程技术网

Java 在android中使用Google map api v2显示我的曲目

Java 在android中使用Google map api v2显示我的曲目,java,android,google-maps,google-maps-api-2,Java,Android,Google Maps,Google Maps Api 2,我正在尝试制作一个应用程序,它将显示我的当前位置,并通过一条线跟踪我。我一直在使用谷歌地图Api v2 for android,所以我尝试使用多段线来帮助我显示我的轨迹,但它没有显示。 有人能帮我吗谢谢。 提供了总代码 public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener, OnM

我正在尝试制作一个应用程序,它将显示我的当前位置,并通过一条线跟踪我。我一直在使用谷歌地图Api v2 for android,所以我尝试使用多段线来帮助我显示我的轨迹,但它没有显示。 有人能帮我吗谢谢。 提供了总代码

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

}
}

这是非常容易在谷歌地图API V2。如果您想这样做,那么您可以遵循以下参考链接:

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

对于你的设施,我写了主要内容:

1.创建车床点列表,例如:

List<LatLng> routePoints;
3.创建一条多段线,并按如下方式向其提供板条点列表:

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

试试这个并给出反馈

您在哪里将
setIt
设置为true?我在“开始”按钮和“继续”按钮上进行了设置。是否成功调用了onLocationChanged?
Polyline route = map.addPolyline(new PolylineOptions()
  .width(_strokeWidth)
  .color(_pathColor)
  .geodesic(true)
  .zIndex(z));
route.setPoints(routePoints);