0

我正在尝试使用 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
4

2 回答 2

0
mMap.setMyLocationEnabled(true); // just add one line it wil work.
于 2013-08-16T12:02:48.393 回答
0

将您的Log.i(latlng.toString(),"Current Location" );代码移动到onLocationChanged- 方法,您在onProviderEnabled.

于 2013-05-07T18:24:21.497 回答