2017-02-27 96 views
0

我难住这个当前问题,我试图从超类中检索用户位置,最终将其分配给标记。绊倒我的是我如何简单地从超类中检索一个位置?我的MapsActivity超类有蓝点,但是如何将其转移过来?Android谷歌地图 - NullPointerException - Location.getLatitude()

mLastLocation是从超

我在正确的轨道上的位置的参考?

我很新,所以任何快速见解将不胜感激!

public class CircleSizeActivity extends MapsActivity implements OnMapReadyCallback, OnMarkerDragListener, LocationListener{ 

    Marker mMarkerA; 
    Marker mMarkerB; 
    Polyline mPolyline; 
    GoogleMap mMap = getMap(); 
    private GoogleApiClient mGoogleApiClient; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_circle); 
     if (mGoogleApiClient == null) { 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API) 
        .build(); 
     } 

    } 

    protected void onStart() { 
     mGoogleApiClient.connect(); 
     super.onStart(); 

    } 

    protected void onStop() { 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
    if (checkLocationPermission() == true) { 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      System.out.println(mLastLocation.getLatitude()); 
      System.out.println(mLastLocation.getLongitude()); 
     } 

     } 
    } 


    @Override 
    public void onMapReady (GoogleMap googleMap){ 
     mMap = googleMap; 
     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     if ((ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || 
      ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)) { 
      mMap.setMyLocationEnabled(true); 

    } 
} 

更新了超

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, LocationListener, OnClickListener, View.OnLongClickListener { 

private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
LocationRequest mLocationRequest; 
Location mLastLocation; 
Marker mCurrLocationMarker; 
Circle circle1; 




CircleOptions circleOptions = new CircleOptions(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map_activity); 
    mapFragment.getMapAsync(this); 







} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
    mGoogleApiClient.connect(); 
} 


@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
    //Initialize Google Play Services 
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(this, 
       android.Manifest.permission.ACCESS_FINE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      buildGoogleApiClient(); 
      mMap.setMyLocationEnabled(true); 
     } 
    } else { 
     buildGoogleApiClient(); 
     mMap.setMyLocationEnabled(true); 
    } 
} 



@Override 
public void onConnected(@Nullable Bundle bundle) { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(1000); 
    mLocationRequest.setFastestInterval(1000); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    if (ContextCompat.checkSelfPermission(this, 
      android.Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 

} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
public void onLocationChanged(Location location) { 
    mLastLocation = location; 
    if (mCurrLocationMarker != null) { 
     mCurrLocationMarker.remove(); 
    } 

    //Place current location marker 
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(latLng); 
    markerOptions.title("Current Position").draggable(true); 
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
    mCurrLocationMarker = mMap.addMarker(markerOptions); 

    //move map camera 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 

    //stop location updates 
    if (mGoogleApiClient != null) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
    } 


} 

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; 

public boolean checkLocationPermission() { 
    if (ContextCompat.checkSelfPermission(this, 
      android.Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Asking user if explanation is needed 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       android.Manifest.permission.ACCESS_FINE_LOCATION)) { 

      // Show an expanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 

      //Prompt the user once explanation has been shown 
      ActivityCompat.requestPermissions(this, 
        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 


     } else { 
      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions(this, 
        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_LOCATION); 
     } 
     return false; 
    } else { 
     return true; 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_LOCATION: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted. Do the 
       // contacts-related task you need to do. 
       if (ContextCompat.checkSelfPermission(this, 
         android.Manifest.permission.ACCESS_FINE_LOCATION) 
         == PackageManager.PERMISSION_GRANTED) { 

        if (mGoogleApiClient == null) { 
         buildGoogleApiClient(); 
        } 
        mMap.setMyLocationEnabled(true); 
       } 

      } else { 

       // Permission denied, Disable the functionality that depends on this permission. 
       Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show(); 
      } 
      return; 
     } 
    } 
} 
+0

你在哪里实际实例'mLastLocation'?我没有看到这个地方。 –

+0

谢谢!刚刚编辑我的帖子以包含MapsActivity。我 – TJBlack31

+0

@TimBiegeleisen谢谢!刚刚编辑我的帖子以包含MapsActivity。 I – TJBlack31

回答

0

我不明白为什么你不会有这一切在一个活动,但我的建议是让公众mLastLocation或保护。

内,您的MapsActivity

public Location mLastLocation; 
+0

考虑到您一次只能打开一个活动,我看不到公共或受保护的成员变量如何提供帮助。 –

+0

这就是事情,一项活动似乎在扩大另一项。 –

+0

在某些情况下有意义,但不是真正显示的代码。 –