我正在尝试创建一个符合用户旅行路线的Android应用。虽然在这一点上我只是试图获得他们当前的位置。我尝试了几个涉及LocationListeners等的方法,但要么得到指南从未提及的结果或错误。有些人在HTML5页面中使用它,但我真的不知道这样做。下面我有一些代码,理论上应该获取用户的位置并在那里放置一个标记。
不可否认,有很多进口商品,主要来自我在这个项目中磕磕绊绊,并尝试不同的位置来解决其他问题。最终结果将是用户完成某事物的外围并且它将获得该区域。虽然从一系列LatLng中获取该区域是可行的,但问题是获得用户走过它的实际周长。我希望,此刻,将用户的位置反复进入LatLng列表并使用它,但我愿意接受其他建议。
package ideo.farmerhelp;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.graphics.Color;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Result;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.common.api.zzc;
import com.google.android.gms.common.api.zzl;
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.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.android.SphericalUtil;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, OnMarkerDragListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, android.location.LocationListener, GoogleMap.OnMapClickListener {
TextView tv;
private GoogleApiClient mGoogleApiClient;
public static final String TAG = MapsActivity.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private LocationRequest mLocationRequest;
private GoogleMap mMap;
SupportMapFragment mapFragment;
TextView txtlat;
String lat;
String provider;
protected String latitude, longitude;
protected boolean gps_Enabled, network_enabled;
@Override
protected void onCreate(Bundle savedInstanceState) { //On map create
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map); //Create a support map fragment
mapFragment.getMapAsync(this);
tv = (TextView) findViewById(R.id.area);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); //Requests that I cast the fourth parameter to android.location.LocationListener, doing so results in a castexception.
}
}