从android中的线程更改地图位置

时间:2015-12-22 21:48:24

标签: java android multithreading google-maps

我正在制作一个关于在地图上找朋友的Android应用程序。到目前为止,我已经启动并运行了地图,并且融合的位置也在运行,但是从不同的类扩展了线程,因此应用程序将在整个生命周期中继续监听位置。

我在初始化地图时遇到问题,问题是locationListener会继续工作但是地图不会加载,或者地图会加载但是locationListener需要很长时间来获取新地点

将线程活动中的当前位置发送到地图活动时,我也遇到问题,因此我将相机设置为新的动作。

以下是我得到的错误:

  

12-22 23:22:38.847 17339-17339 / com.example.swampsoftware.blbmonitor   W / dalvikvm:threadid = 1:线程退出未捕获的异常   (组= 0x41ee6300)12-22 23:22:38.852   17339-17339 / com.example.swampsoftware.blbmonitor E / AndroidRuntime:   致命异议:主要                                                                                         显示java.lang.NullPointerException                                                                                             在   com.example.swampsoftware.blbmonitor.Maps.gotoLocation(Maps.java:232)                                                                                             在   com.example.swampsoftware.blbmonitor.GetLocation.onLocationChanged(GetLocation.java:81)                                                                                             在   com.google.android.gms.location.internal.zzk $ zzb.handleMessage(未知   资源)                                                                                             在android.os.Handler.dispatchMessage(Handler.java:99)                                                                                             在android.os.Looper.loop(Looper.java:137)                                                                                             在android.app.ActivityThread.main(ActivityThread.java:4898)                                                                                             at java.lang.reflect.Method.invokeNative(Native Method)                                                                                             在java.lang.reflect.Method.invoke(Method.java:511)                                                                                             在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1006)                                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)                                                                                             在dalvik.system.NativeStart.main(本地方法)

一旦方法" onLocationChanged"就会出现问题。被称为。

我认为这是线程引起的问题,但我不明白问题是什么,也不知道如何修复它,这是我做的代码:

Maps.class:

public class Maps extends FragmentActivity implements OnMapReadyCallback{


private GoogleMap mMap;

private Double locationLat;
private Double locationLng;

private Context context;


private static boolean isVisible;

// Defining parse object
ParseObject locationObject;

//Declaring Tabhost
TabHost tabHost;

public Maps() {

}

public Maps(Context context) {
    this.context = context;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    isVisible = true;     
    setContentView(R.layout.activity_gpslocation);

    // Initialize google map
    initMap();

}


// Obtain the SupportMapFragment and get notified when the map is ready to be used.
// Initialize GoogleMap -> mMap
public void initMap() {

    if (mMap == null) {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    } else {
        Toast.makeText(getApplicationContext(), "Can't start map", Toast.LENGTH_SHORT).show();
    }
}



@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;  
}

// Method that saves current location
private void setCurrentLocation(Double lat, Double lng) {

    locationLat = lat;
    locationLng = lng;
}

// Get method for lng
public Double getLocationLat() {
    return locationLat;
}

public Double getLocationLng() {
    return locationLng;
}


// Method that animates the camera to a new location
public void gotoLocation(Location location) {

    LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, 15);
    mMap.animateCamera(update);
}

@Override
protected void onResume() {
    super.onResume();
    initMap();
    isVisible = true;
}

@Override
protected void onPause() {
    super.onPause();   
    isVisible = false;
}

protected void onDestroy() {
    super.onDestroy();
    isVisible = false;
}

public boolean getIsVisible() {
    return isVisible;
}

// Method that is called when map is connected, initializes the locationRequest.
public void onConnected(Bundle bundle) {


}

这是Thread类,它将使用融合位置继续监听新位置:

GetLocation:

public class GetLocation extends Thread implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient googleApiClient; // Object that is used to connect to google maps API, must be built to use fused location
private LocationRequest locationRequest; //Object that requests a quality of service for location updates from fused location
public Context context;
public volatile boolean isRunning = true; // boolean to stop the thread loop

// Declaring location variables
private Double locationLat;
private Double locationLng;

// Declaring objects
private ParseData parseData;
private Maps maps = new Maps();
/***
 * run method for thread
 ***/
public void run() {

    //Initializing objects
    parseData = new ParseData();

    googleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    googleApiClient.connect();
}

public void terminate() {

    isRunning = false;
}

// Getters
public Double getLocationLat() {
    return locationLat;
}

public Double getLocationLng() {
    return locationLng;
}


/***
 * Auto-generated methods
 ***/
@Override
public void onLocationChanged(Location location) {

    if (!isRunning) {
        googleApiClient.disconnect();
        return;
    } else if (location != null) {
        locationLat = location.getLatitude();
        locationLng = location.getLongitude();
        parseData.GetLocationParse(locationLat, locationLng);
        System.out.println(locationLat + " " + locationLng);

        // Check is map is active
        if(maps.getIsVisible()) {
            System.out.println("Maps is in foreground");
            maps.gotoLocation(location);
        } else {
            System.out.println("Maps isn't running");
        }
    }


}

@Override
public void onConnected(Bundle bundle) {

    locationRequest = locationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(7000);
    locationRequest.setFastestInterval(5000);
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

谢谢。

0 个答案:

没有答案