找不到用户位置:

时间:2013-04-01 15:26:41

标签: google-maps android-maps-v2

我想我已经完成了所有使用Google Maps API v2但未显示位置且相机不会改变其位置。

地图正常加载但仅保留在0,0位置且永远不会移动。

在设备中,我看到GPS信号仅查找位置,我已在外面测试过。

这是我的代码: MainActivity.java:

    public class MainActivity extends FragmentActivity implements LocationSource,LocationListener, OnMapClickListener, OnMyLocationChangeListener
    {

         final int RQS_GooglePlayServices = 1;
         private GoogleMap myMap;
         private LocationManager lm;
         public Location myLocation;
         public TipoBusca busca;
         private enum TipoBusca {BUSCA_PARADA, BUSCA_LOCALIZACAO_INICIAL, BUSCA_LOCALIZACAO, BUSCA_ENDERECO, BUSCA_DRAG};
         public String tipoRequest;
         private Criteria myCriteria;
         public TextView textView;
         public OnMyLocationChangeListener locationListener;

         @Override
         protected void onCreate(Bundle savedInstanceState)
         {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_main);
                     //Define textView wich will receive test messages
                 textView = (TextView) findViewById(R.id.textView1);

                 // Getting Google Play availability status
                     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

                    // Showing status
                    if(status!=ConnectionResult.SUCCESS)
                    { // Google Play Services are not available
                        int requestCode = 10;
                        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
                        dialog.show();
                    }
                    else
                    {
                              //Defining fragment for map
                          FragmentManager myFragmentManager = getSupportFragmentManager();
                  SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
                  myMap = mySupportMapFragment.getMap();
                  myMap.setMyLocationEnabled(true);
                  myMap.setIndoorEnabled(true);
                  myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                  myMap.setLocationSource(this);
                  //myMap.setOnMyLocationChangeListener(this);
                  myMap.setOnMyLocationChangeListener((OnMyLocationChangeListener) locationListener);

                  myCriteria = new Criteria();
                  myCriteria.setAccuracy(Criteria.ACCURACY_FINE);

                  lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);             
                  //lm.requestLocationUpdates(0, 50.0f, myCriteria, this, null);
                  lm.requestLocationUpdates(250, 1, myCriteria, this, null);

                  textView.setText("Localizando usuário...");
                  myLocation = myMap.getMyLocation();
                      }
           }

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

}

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

}

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

}

@Override
public void activate(OnLocationChangedListener listener) {
    // TODO Auto-generated method stub

}

@Override
public void deactivate() {
    // TODO Auto-generated method stub

}

@Override
public void onMapClick(LatLng point) {
    // TODO Auto-generated method stub
    myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
}

@Override
public void onMyLocationChange(Location location) {
    // TODO Auto-generated method stub
    Log.i("onMyLocationChanged", "my location changed");
    LatLng latlng= new LatLng(location.getLatitude(), location.getLongitude());
    myMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    myMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    textView.setText("Latitude:" +  location.getLatitude()  + ", Longitude:" + location.getLongitude() );
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    Log.i("onLocationChanged", "location changed");
}
}

清单:

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myPackage"
android:versionCode="1"
android:versionName="1.0" >
          <!-- Setting Permissions -->
          <permission 
    android:name="myPackage.permission.MAPS_RECEIVE" android:protectionLevel="signature"></permission>
          <uses-permission android:name="myPackage.permission.MAPS_RECEIVE"/>
          <!-- Setting versions requirements -->
          <uses-sdk 
                  android:minSdkVersion="8" 
                  android:targetSdkVersion="17" />

          <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
          <uses-permission android:name="android.permission.INTERNET"/>
          <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
          <!-- External storage for caching. -->
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
          <!-- My Location -->
          <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
          <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
          <!-- Maps API needs OpenGL ES 2.0. -->
          <uses-feature 
                      android:glEsVersion="0x00020000" 
                      android:required="true"/>

          <!-- setting icon for application -->
          <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" >

              <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="myApiKey_Code_Inserted_here"/>

              <activity android:name="myPackage.MainActivity" >
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />

                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
          </application>

      </manifest>

1 个答案:

答案 0 :(得分:0)

如果您要完成的是在用户(我的位置点)更改位置时让相机自动更新,那么需要以下内容:

  1. 实施OnMyLocationChangeListener
  2. myMap.setMyLocationEnabled(true)(启用my-location图层,其中包含内置位置提供程序)
  3. myMap.setOnMyLocationChangeListener(this)(注册以便在我的位置点更改位置时接收更新)
  4. 在您的回调方法onMyLocationChange(Location location)中相应地更新相机。
  5. 你已经拥有了所有这些的代码,但是我看到你在步骤3中对该行进行了评论,这可能是你没有看到相机更新的原因。

    您无需实施LocationSourceLocationListener(因为我的位置图层有自己的位置提供商),只有在您想要回复时才需要OnMapClickListener用户点击地图上的某个点。