Android如何关注当前位置

reg*_*eme 4 android location google-maps

嗨,我有一个Android应用程序,可以在Google地图上找到您的位置,但是当我启动该应用程序时,它是从非洲开始的,而不是我当前所在的城市,国家,位置等。我已经在developer.android.com上检查了信息与位置问题有关,但问题仍然存在。

这是代码;有任何想法吗?谢谢..

  package com.kodlab.nerdeyim;

  import android.location.Location;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;

   import com.google.android.gms.common.ConnectionResult; 
   import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
   import  com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
   import com.google.android.gms.location.LocationClient;
   import com.google.android.gms.location.LocationListener;
   import com.google.android.gms.location.LocationRequest;
   import com.google.android.gms.maps.CameraUpdateFactory;
   import com.google.android.gms.maps.GoogleMap;
   import com.google.android.gms.maps.SupportMapFragment;
   import com.google.android.gms.maps.model.LatLng;

   public class MainActivity extends FragmentActivity implements ConnectionCallbacks,    OnConnectionFailedListener, LocationListener {

private LocationClient locationClient;
private LocationRequest locationRequest;
private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationClient = new LocationClient(this, this, this);

    locationRequest = LocationRequest.create()
              .setInterval(5000)
              .setFastestInterval(500)
              .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    googleMap = supportMapFragment.getMap();
    googleMap.setMyLocationEnabled(true);   
}

@Override
public void onLocationChanged(Location location) {
    HaritadaKonumGosterAsyncTask task = new HaritadaKonumGosterAsyncTask();
    task.execute(new Location[] {location});
}

@Override
public void onConnected(Bundle connectionHint) {
    locationClient.requestLocationUpdates(locationRequest, this);
}

@Override
public void onDisconnected() {}

@Override
public void onConnectionFailed(ConnectionResult result) {}

@Override
protected void onResume() {
    super.onResume();
    locationClient.connect();
}

@Override
public void onPause() {
    super.onPause();

    if(locationClient.isConnected())
        locationClient.removeLocationUpdates(this);

    locationClient.disconnect();
}

private class HaritadaKonumGosterAsyncTask  extends AsyncTask<Location, Void, LatLng> {

    @Override
    protected LatLng doInBackground(Location... params) {
        Location konum = params[0];
        return new LatLng(konum.getLatitude(), konum.getLongitude());
    }

    @Override
    protected void onPostExecute(LatLng konum) {
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(konum, 15));
    }

}


 }
Run Code Online (Sandbox Code Playgroud)

小智 5

感谢您的代码解决了我的问题,现在我来帮忙

public void onLocationChanged(Location location) 
{
  // Getting latitude of the current location
  double latitude = location.getLatitude();

  // Getting longitude of the current location
  double longitude = location.getLongitude();

  float speed = location.getSpeed();

  // Creating a LatLng object for the current location
  LatLng latLng = new LatLng(latitude, longitude);

  // Showing the current location in Google Map
  CameraPosition camPos = new CameraPosition.Builder()
    .target(new LatLng(latitude, longitude))   
    .zoom(18)        
    .bearing(location.getBearing())
    .tilt(70)
    .build();
  CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);
  googleMap.animateCamera(camUpd3);
}
Run Code Online (Sandbox Code Playgroud)


jas*_*rty 5

您也可以在XML布局中进行设置(设置北美的中度设置):

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraBearing="0"
    map:cameraTargetLat="53.938305"
    map:cameraTargetLng="-112.763672"
    map:cameraTilt="30"
    map:cameraZoom="2" />
Run Code Online (Sandbox Code Playgroud)

在该示例中,我使用的是android maps扩展。很棒的图书馆顺便说一句。我真的很喜欢这种方法,因为它立即载入,您再也看不到非洲。IMO似乎是最好的。

注意!我应该警告您,直到完成空间后,我才会遇到编译错误,然后在完成一天的工作后启动Eclipse时再次保存该文件。有人知道这是为什么吗?