【问题标题】:Google Map API 2 not showning current locationGoogle Map API 2 未显示当前位置
【发布时间】:2013-08-16 20:02:48
【问题描述】:

我正在尝试使用 android google map api 2 显示当前位置,我在启用 wifi 的手机上运行它。

但它总是显示经度和纬度 0.0 , 0.0 。

这是我的代码

   package com.google.map;

   import com.google.android.gms.common.ConnectionResult;
   import com.google.android.gms.common.GooglePlayServicesUtil;
   import com.google.android.gms.maps.CameraUpdateFactory;
   import com.google.android.gms.maps.GoogleMap;
   import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
   import com.google.android.gms.maps.MapFragment;
   import com.google.android.gms.maps.model.CameraPosition;
   import com.google.android.gms.maps.model.LatLng;
   import com.google.map.R;

   import android.location.Criteria;
   import android.location.Location;
   import android.location.LocationListener;
   import android.location.LocationManager;
   import android.os.Bundle;
   import android.annotation.SuppressLint;
   import android.app.Activity;
   import android.app.Dialog;
   import android.app.FragmentManager;
   import android.content.Intent;
   import android.util.Log;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.widget.Toast;

   public class MainActivity extends Activity implements LocationListener{

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

    if(isGooglePlay())
    {
        setContentView(R.layout.activity_main);
        setUpMap();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if(item.getItemId() == R.id.action_legalnotices)
    {
        startActivity(new Intent(this, LegalNoticesActivity.class));
    }
    return super.onOptionsItemSelected(item);
}

private boolean isGooglePlay()
{
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if(status == ConnectionResult.SUCCESS)
    {
        return true;
    }
    else
    {
        ((Dialog)GooglePlayServicesUtil.getErrorDialog(status, this, 10)).show();
        Toast.makeText(getApplicationContext(), "Google play is not available.", Toast.LENGTH_LONG).show();
    }
    return false;
}

@SuppressLint("NewApi") private void  setUpMap()
{
    if(googlemap == null)
    {
        googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
        if(googlemap != null)
        {
            // map code
            googlemap.setMyLocationEnabled(true);

            googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            LocationManager lo = (LocationManager) getSystemService(LOCATION_SERVICE);

            String provider = lo.getBestProvider(new Criteria(),true);


            if(provider == null)
            {
                onProviderDisabled(provider);
            }
            else
            {
                onProviderEnabled(provider);
            }

            Location loc = lo.getLastKnownLocation(provider);

            if(loc != null)
            {
                onLocationChanged(loc);
            }


            googlemap.setOnMapLongClickListener(onLongClickMapSettings());
        }
    }
}


private OnMapLongClickListener onLongClickMapSettings() {
    // TODO Auto-generated method stub
    return new OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng arg0) {
            // TODO Auto-generated method stub
            Log.i(arg0.toString(),"Long Click");
        }
    };
}


@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude());

    googlemap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    googlemap.moveCamera(CameraUpdateFactory.zoomTo(5));

}


@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}


@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Location lo = new Location(provider);

    LatLng latlng = new LatLng(lo.getLatitude(),lo.getLongitude());

    Log.i(latlng.toString(),"Current Location" );
    googlemap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 5));

}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
   }

这是我的 android 应用程序的 Log cat 值。

 05-07 22:16:31.636: I/lat/lng: (0.0,0.0)(17967): Current Location

【问题讨论】:

  • 已将其添加到清单文件
  • 它在您更改位置时起作用,这意味着将您的设备移动一小段距离,然后自动调用 onchangelocation()。
  • 您的手机是否启用了定位服务?
  • 是的,我添加了这些权限。我按照本教程在开始时在 android 手机上显示地图。 developers.google.com/maps/documentation/android/start
  • 定位服务也为应用启用

标签: android


【解决方案1】:

将您的Log.i(latlng.toString(),"Current Location" ); 代码移动到onLocationChanged 方法,您在onProviderEnabled 中还没有位置。

【讨论】:

    【解决方案2】:
    mMap.setMyLocationEnabled(true); // just add one line it wil work.
    

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
    • Google map api v2 为我们提供了这种功能。当用户位置发生变化时,它会自动更新。如果禁用,它将设置为最后一个位置......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 2014-03-25
    相关资源
    最近更新 更多