本文介绍了distanceTo(map.getMyLoctation())返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和我使用​​与LocationListener的和谷歌地图API第2版相关联的位置经理,我想获得用户的当前位置,另一个位置之间的距离,但我总是在地图上的空指针。 getMyLocation()。

这是我的code:

 的LocationManager locManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyLocationListener locListener =新MyLocationListener(本);
雅格=(按钮)findViewById(R.id.button1);
rotebuhlplatz =(按钮)findViewById(R.id.button2);
rotebuhlst =(按钮)findViewById(R.id.button3);如果(locListener.canGetLocation){    双MLAT = locListener.getLatitude();
    双mLong = locListener.getLongitude();}其他{
    //无法获取位置
}
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locListener);
地图=((MapFragment)getFragmentManager()findFragmentById(R.id.map)。)的GetMap();
    标记雅格= map.addMarker(新的MarkerOptions()位置(DHBWJager56)
        .title伪(DHBWJägerstraße大街56)
       .icon(BitmapDesc​​riptorFactory.defaultMarker(BitmapDesc​​riptorFactory.HUE_GREEN)));    allMarkersMap.put(雅格,Jager56.class);
    map.setOnInfoWindowClickListener(本);    标记jager2 = map.addMarker(新的MarkerOptions()位置(DHBWJager58)
        .title伪(DHBWJägerstraße大街58)
       .icon(BitmapDesc​​riptorFactory.defaultMarker(BitmapDesc​​riptorFactory.HUE_GREEN)));
    allMarkersMap.put(雅格,Jager58.class);
    map.setOnInfoWindowClickListener(本);
    标记rotebuhl = map.addMarker(新的MarkerOptions()
        .POSITION(DHBWRotebuhl)
        .title伪(DHBWRotebühlplatz41/1)图标(BitmapDesc​​riptorFactory.defaultMarker(BitmapDesc​​riptorFactory.HUE_AZURE)))。
    allMarkersMap.put(rotebuhl,Rotebuhl.class);
    map.setOnInfoWindowClickListener(本);
      // .icon(BitmapDesc​​riptorFactory
        // .fromResource(R.drawable.ic_launcher)));    标记RTS = map.addMarker(新的MarkerOptions()位置(DHBWRotebuhlstrasse)
            .title伪(DHBWRotebühlstraße131));
    allMarkersMap.put(RTS,SocialWork.class);
    map.setOnInfoWindowClickListener(本);
    //瞬间移动相机Jägerstraße大街为15的缩放。
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(DHBWRotebuhl,17.0f));    //放大,动画摄像机。
    map.animateCamera(CameraUpdateFactory.zoomTo(14),2000,NULL);
    map.setMyLocationEnabled(真);
    位置L1 =新的位置(源);
    l1.setLatitude(DHBWJager56.latitude);
    l1.setLongitude(DHBWJager56.longitude);
    浮F = l1.distanceTo(map.getMyLocation());


解决方案

很高兴见到您正在使用谷歌地图API V2的工作。

下面是对的GoogleMap文档的链接。

在那里你会看到,getMyLocation()API是pcated德$ P $。他们建议您使用LocationClient。一旦你的当前位置,使用相同的l1.distanceTo(locationClient.getLastLocation()),你应该是好去。希望这有助于

从文件摘录:

Sample Code they suggest:

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

  private GoogleMap mMap;

  private LocationClient mLocationClient;
  private TextView mMessageView;

  // These settings are the same as the settings for the map. They will in fact give you updates at
  // the maximal rates currently possible.
  private static final LocationRequest REQUEST = LocationRequest.create()
      .setInterval(5000)         // 5 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);
    mMessageView = (TextView) findViewById(R.id.message_text);
  }

  @Override
  protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
    setUpLocationClientIfNeeded();
    mLocationClient.connect();
  }

  @Override
  public void onPause() {
    super.onPause();
    if (mLocationClient != null) {
      mLocationClient.disconnect();
    }
  }

  private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
      // Try to obtain the map from the SupportMapFragment.
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
             .getMap();
      // Check if we were successful in obtaining the map.
      if (mMap != null) {
        mMap.setMyLocationEnabled(true);
      }
    }
  }

  private void setUpLocationClientIfNeeded() {
    if (mLocationClient == null) {
      mLocationClient = new LocationClient(
          getApplicationContext(),
          this,  // ConnectionCallbacks
          this); // OnConnectionFailedListener
    }
  }

  /**
   * 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();
    }
  }

  /**
   * Implementation of {@link LocationListener}.
   */
  @Override
  public void onLocationChanged(Location location) {
    mMessageView.setText("Location = " + location);
  }

  /**
   * Callback called when connected to GCore. Implementation of {@link ConnectionCallbacks}.
   */
  @Override
  public void onConnected(Bundle connectionHint) {
    mLocationClient.requestLocationUpdates(
        REQUEST,
        this);  // LocationListener
  }

  /**
   * 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
  }
}

这篇关于distanceTo(map.getMyLoctation())返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:52