开发者

current location is not working in android emulator

开发者 https://www.devze.com 2023-02-05 21:00 出处:网络
I am following a tutorial to get my current location. I wrote exactly what the tutorial said, but i just do not get my lat/lon. I am using a wifi connection. I tried:

I am following a tutorial to get my current location. I wrote exactly what the tutorial said, but i just do not get my lat/lon. I am using a wifi connection. I tried:

geo fix -77.036519 38.896143

still do not get the lat/lon. My code is as follows;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.lo开发者_StackOverflow中文版cation.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;

public class TestProviderController extends Activity {
 LocationManager locationManager;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String location_context=Context.LOCATION_SERVICE;
        locationManager=(LocationManager)getSystemService(location_context);
        testProvider();
    }
    public void testProvider(){
     TextView tv = (TextView)findViewById(R.id.myTextView);
     StringBuilder sb = new StringBuilder("Enabled Providers:");

     List<String> providers = locationManager.getProviders(true);

     for (String provider : providers) {

     locationManager.requestLocationUpdates(provider, 1000, 0,
     new LocationListener() {
     public void onLocationChanged(Location location) {}
     public void onProviderDisabled(String provider){}

     public void onProviderEnabled(String provider){}
     public void onStatusChanged(String provider, int status,
     Bundle extras){}
     });
     sb.append("\n").append(provider).append(":");
     Location location = locationManager.getLastKnownLocation(provider);
     if (location != null) {
     double lat = location.getLatitude();
     double lng = location.getLongitude();
     sb.append(lat).append(",").append(lng);
     } else {
     sb.append("No Location");
     }
     }
     tv.setText(sb);
    }
}

Can anybody please help about what went wrong?


It can apparently take a little time to get the coordinates - here's a link to a similar question

0

精彩评论

暂无评论...
验证码 换一张
取 消