Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.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
我在android中的位置坐标_Android_Gps_Location_Maps - Fatal编程技术网

我在android中的位置坐标

我在android中的位置坐标,android,gps,location,maps,Android,Gps,Location,Maps,我正试图从gps坐标中获取我的当前位置。。我看到了很多我应该如何做到这一点的例子。当我尝试将我的代码与示例代码合并时,我在这一行得到一个eror: lm.requestLocationUpdates(provider, 1, 0, locationListener); 上面写着: The method requestLocationUpdates(String, long, float, LocationListener) in the type LocationManager is not

我正试图从gps坐标中获取我的当前位置。。我看到了很多我应该如何做到这一点的例子。当我尝试将我的代码与示例代码合并时,我在这一行得到一个eror:

lm.requestLocationUpdates(provider, 1, 0, locationListener);
上面写着:

The method requestLocationUpdates(String, long, float, LocationListener) in the type LocationManager is not applicable for the arguments (String, int, int, LocationListener)
哪里会有问题。。?花几个小时寻找问题

public class MyLocationDemoActivity extends FragmentActivity implements
        ConnectionCallbacks,
        OnConnectionFailedListener, 
        LocationListener,
        OnMyLocationButtonClickListener{
    GMapV2Direction md;
    private GoogleMap mMap;
    GeoPoint gp;
    private LocationClient mLocationClient;
    LocationManager lm;
    private TextView mMessageView;
    Bundle extras;
    Location mycurrent;
    LatLng endpoint;
    Location mCurrentLocation;
    static double currentlatid;
    static double currentlonid;

    private static final LocationRequest REQUEST = LocationRequest.create()
            .setInterval(16000) // 16 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);
        Intent intent = getIntent();
        RouteActivity route = new RouteActivity();


        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationListener = new MyLocationListener();
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = lm.getBestProvider(criteria, true);
        Location mostRecentLocation = lm.getLastKnownLocation(provider);
        if(mostRecentLocation != null) {
            currentlatid=mostRecentLocation.getLatitude();
            currentlonid=mostRecentLocation.getLongitude();
        }
        lm.requestLocationUpdates(provider, 1, 0, locationListener);

        extras = getIntent().getExtras();

        LatLng a = null;
        try {
            a = (LatLng) extras.get("coord");
        } catch (Exception x) {
        }
        if (a == null) {
            try {

                LatLng x = new LatLng(mycurrent.getLatitude(),
                        mycurrent.getLongitude());
                a = x;
                System.out.print(a);
            } catch (Exception x) {

            }

            System.out.print(a);
        }
        LatLng b ;
        try{
        b = (LatLng) extras.get("Obj1");
        endpoint = b;
        draw_route(a, b);
        }catch(Exception e){

        }

        LatLng c = null;
        try {
            c = (LatLng) extras.get("Obj2");
            endpoint = c;
            b = (LatLng) extras.get("Obj1");
            draw_route(b, c);

        } catch (Exception e) {
            System.out.println("smth went wrong");
        }
        try {
            LatLng d = (LatLng) extras.get("Obj3");
            endpoint = d;
            draw_route(c, d);

        } catch (Exception e) {
            System.out.println("smth went wrong");
        }

        // mMap.addMarker(new MarkerOptions().position(a));

        try{
            mMap.setMyLocationEnabled(true);
            mMap.setOnMyLocationButtonClickListener(this);
        }catch(Exception e ){

        }


    }

    // }

    private final LocationListener locationListener = new LocationListener() {

        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {}

        public void onStatusChanged(String provider,int status,Bundle extras){}
    };
    private void updateWithNewLocation(Location location) {
       // TextView myLocationText = (TextView) findViewById(R.id.text);
        String latLongString = "";
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;
        } else {
            latLongString = "No location found";
        }
       // myLocationText.setText("Your Current Position is:\n" + latLongString);
    }
    private void setUpLocationClientIfNeeded() {
        if (mLocationClient == null) {
            mLocationClient = new LocationClient(getApplicationContext(), this, // ConnectionCallbacks
                    this); // OnConnectionFailedListener
        }
    }

    public void draw_route(LatLng start, LatLng finish) {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        md = new GMapV2Direction();
        mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        LatLng coordinates = new LatLng(54.72, 25.3);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 10));

        mMap.addMarker(new MarkerOptions().position(start).title("Start"));
        mMap.addMarker(new MarkerOptions().position(finish).title("End"));

        Document doc = md.getDocument(start, finish,
                GMapV2Direction.MODE_DRIVING);
        int duration = md.getDurationValue(doc);
        String distance = md.getDistanceText(doc);
        String start_address = md.getStartAddress(doc);
        String copy_right = md.getCopyRights(doc);

        ArrayList<LatLng> directionPoint = md.getDirection(doc);
        PolylineOptions rectLine = new PolylineOptions().width(3).color(
                Color.RED);

        for (int i = 0; i < directionPoint.size(); i++) {
            rectLine.add(directionPoint.get(i));
        }

        mMap.addPolyline(rectLine);
    }

    /**
     * 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();
            mycurrent = mLocationClient.getLastLocation();


        }
    }

    /**
     * Implementation of {@link LocationListener}.
     */
    @Override
    public void onLocationChanged(Location location) {
        mMessageView.setText("Location = " + location);
        Toast.makeText(this, "MyLocation button clicked", 50000000)
        .show();
        draw_route(new LatLng(location.getLatitude(), location.getLongitude()), endpoint);
    }

    /**
     * Callback called when connected to GCore. Implementation of
     * {@link ConnectionCallbacks}.
     */
    @Override
    public void onConnected(Bundle connectionHint) {
        mLocationClient.requestLocationUpdates(REQUEST, this); // LocationListener
        mCurrentLocation = mLocationClient.getLastLocation();
         System.out.println("@@@@@@@@@@@@@\n"+mCurrentLocation+"\n@@2222222@@@@@@@@@@@``");
    }

    /**
     * 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() {
        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;
    }
    class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
          if (loc != null) {
              currentlatid = loc.getLatitude();
            currentlonid = loc.getLongitude();

            float accuracyd = loc.getAccuracy();
            String providershown = loc.getProvider();    



           }
        }
}}
公共类MyLocationDemoActivity扩展了FragmentActivity实现
连接回调,
OnConnectionFailedListener,
LocationListener,
OnMyLocationButtonClickListener{
GMAPv2定向md;
私有谷歌地图;
地质点gp;
私人地点客户M地点客户;
位置经理lm;
私有文本视图mMessageView;
捆绑附加;
位置电流;
板条终点;
位置mCurrentLocation;
静态双电流;
静态双电流;
私有静态最终位置请求=LocationRequest.create()
.setInterval(16000)//16秒
.setFastTestInterval(16)//16ms=60fps
.setPriority(位置请求。优先级高精度);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.my_location_demo);
Intent=getIntent();
RouteActivity route=新的RouteActivity();
lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
locationListener=新建MyLocationListener();
标准=新标准();
标准.设定准确度(标准.准确度/精细度);
字符串提供程序=lm.getBestProvider(条件为true);
Location mostRecentLocation=lm.getlastnownlocation(提供者);
if(mostRecentLocation!=null){
currentlatid=mostRecentLocation.getLatitude();
currentlonid=mostRecentLocation.getLongitude();
}
lm.RequestLocationUpdate(提供程序,1,0,locationListener);
extras=getIntent().getExtras();
LatLng a=null;
试一试{
a=(LatLng)额外获得(“协调”);
}捕获(异常x){
}
如果(a==null){
试一试{
LatLng x=新LatLng(mycurrent.getLatitude(),
mycurrent.getLongitude());
a=x;
系统输出打印(a);
}捕获(异常x){
}
系统输出打印(a);
}
拉丁语b;
试一试{
b=(LatLng)额外获取(“Obj1”);
终点=b;
绘制路线(a、b);
}捕获(例外e){
}
LatLng c=null;
试一试{
c=(LatLng)额外获取(“Obj2”);
终点=c;
b=(LatLng)额外获取(“Obj1”);
绘制路线(b、c);
}捕获(例外e){
System.out.println(“smth出错”);
}
试一试{
LatLng d=(LatLng)额外获取(“Obj3”);
终点=d;
绘制路线(c,d);
}捕获(例外e){
System.out.println(“smth出错”);
}
//mMap.addMarker(新的MarkerOptions().position(a));
试一试{
mMap.setMyLocationEnabled(真);
mMap.setOnMyLocationButtonClickListener(此);
}捕获(例外e){
}
}
// }
私有最终位置Listener LocationListener=新位置Listener(){
已更改位置上的公共无效(位置){
updateWithNewLocation(位置);
}
公共无效onProviderDisabled(字符串提供程序){
updateWithNewLocation(空);
}
公共无效onProviderEnabled(字符串提供程序){}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){}
};
私有void updateWithNewLocation(位置){
//TextView myLocationText=(TextView)findViewById(R.id.text);
字符串latLongString=“”;
如果(位置!=null){
双纬度=location.getLatitude();
double lng=location.getLongitude();
LATLONSTRING=“Lat:+Lat+”\nLong:+lng;
}否则{
latlonString=“未找到任何位置”;
}
//myLocationText.setText(“您当前的位置是:\n”+latLongString);
}
私有void setupLocationClientIfNeed(){
if(mLocationClient==null){
mLocationClient=new LocationClient(getApplicationContext(),this,//ConnectionCallbacks
此);//OnConnectionFailedListener
}
}
公共空位绘制路线(LatLng开始、LatLng结束){
如果(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
}
md=新的GMapV2Direction();
mMap=((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
车床坐标=新车床坐标(54.72,25.3);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(坐标,10));
mMap.addMarker(新的MarkerOptions().position(start).title(start));
mMap.addMarker(新的MarkerOptions().position(finish.title)(“End”);
Document doc=md.getDocument(开始、完成、,
GMAPv2方向模式(驱动);
int duration=md.getDurationValue(doc);
字符串距离=md.getDistanceText(doc);
字符串start_address=md.getStartAddress(doc);
字符串copy_right=md.getCopyRights(doc);
ArrayList directionPoint=md.getDirection(doc);
PolylineOptions rectLine=新的PolylineOptions().宽度(3).颜色(
颜色(红色);
对于(int i=0;i1. android.location.LocationListener
2. com.google.android.gms.location.LocationListener
    requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
     long minTime = (long)1; 
     float minDistance = (float)0; 
     lm.requestLocationUpdates(provider, 1, 0, locationListener);