获取经度和纬度

Posted

技术标签:

【中文标题】获取经度和纬度【英文标题】:Get longitude and latitude 【发布时间】:2016-09-29 11:03:13 【问题描述】:

大家好,我正在构建一个应用程序,我需要以获取我设法获得的某些功能,但我无法将其值传递给片段中的 OnViewCreated 方法。您可以在 OnviewCreated 中看到这 2 条简单的 toast 消息,它们每次都返回 null

定位器片段

public class LocatorFragment extends Fragment implements OnMapReadyCallback , GoogleApiClient.ConnectionCallbacks 

private MapView mapView;
private GoogleMap map;
private GoogleApiClient mGoogleApiClient;
private  Double Latitude;
private   Double Longitude;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    Intent intent = new Intent(getContext(), GPSTrackerActivity.class);
    startActivityForResult(intent,1);
    View v = inflater.inflate(R.layout.locator_fragment, container, false);


    // Gets the MapView from the XML layout and creates it

    mapView = (MapView) v.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff

    map = mapView.getMap();
    Location mLastLocation;
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    map.getUiSettings().setMyLocationButtonEnabled(false);
    if (ActivityCompat.checkSelfPermission(getContext(), M anifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED        && ActivityCompat.checkSelfPermission(getContext(),      Manifest.permission.ACCESS_COARSE_LOCATION) !=    PackageManager.PERMISSION_GRANTED) 
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode,    String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the    documentation
        // for ActivityCompat#requestPermissions for more details.


        return v;



    
    map.setMyLocationEnabled(true);

    // Needs to call MapsInitializer before doing any CameraUpdateFactory     calls

    MapsInitializer.initialize(this.getActivity());

    // Updates the location and zoom of the MapView


return v;


@Override
public void onResume() 
    mapView.onResume();
    super.onResume();


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


@Override
public void onDestroy() 
    super.onDestroy();
    mapView.onDestroy();


@Override
public void onLowMemory() 
    super.onLowMemory();
    mapView.onLowMemory();


@Override
public void onMapReady(GoogleMap googleMap) 



@Override
public void onConnected(@Nullable Bundle bundle) 



@Override
public void onConnectionSuspended(int i) 


 public void onActivityResult(int requestCode, int resultCode, Intent data) 
     Double longitude;
     Double latitude;
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1)
        Bundle extras = data.getExtras();
         longitude = extras.getDouble("Longitude");
        latitude = extras.getDouble("Latitude");
        Latitude=latitude;
        Longitude=longitude;


    

@Override
public void onViewCreated(View view, Bundle savedInstanceState) 
    Toast.makeText(getContext(),String.valueOf(Latitude),Toast.LENGTH_SHORT).show();
    Toast.makeText(getContext(),String.valueOf(Longitude),Toast.LENGTH_SHORT).show();

        ;

这是我的 GPSTracker 活动

  public class GPSTrackerActivity extends AppCompatActivity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener 

private GoogleApiClient mGoogleApiClient;
Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

    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 bundle) 
    try 

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) 
            Intent intent = new Intent();
            intent.putExtra("Longitude", mLastLocation.getLongitude());
            intent.putExtra("Latitude", mLastLocation.getLatitude());
            setResult(1,intent);
            finish();

        
     catch (SecurityException e) 

    



@Override
public void onConnectionSuspended(int i) 



@Override
public void onConnectionFailed(ConnectionResult connectionResult) 


【问题讨论】:

确保 onActivityResult 被调用。 @override 注释在 onactvityresult() 方法中不存在。那么使用 setResult() 调用它时如何调用它?? @Spirit check this 答案,非常适合我。 【参考方案1】:

我设法通过以下功能做到这一点

map = mapView.getMap();
    map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() 
        @Override
        public void onMyLocationChange(Location location) 
            Latitude= location.getLatitude();
            Longitude=  location.getLongitude();
            Location locationzoom = map.getMyLocation();
            LatLng mapCenter = new LatLng(location.getLatitude(), location.getLongitude());
            CameraPosition cameraPosition = CameraPosition.builder()
                    .target(mapCenter)
                    .zoom(13)
                    .build();
            map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),
                    2000, null);

【讨论】:

以上是关于获取经度和纬度的主要内容,如果未能解决你的问题,请参考以下文章

获取最近的纬度和经度

从地理编码纬度和经度获取地址

在PHP中获取用户机器的经度和纬度[重复]

获取视口两侧的纬度和经度

PHP - 从地址获取纬度和经度[关闭]

如何获取纬度和经度值?