0

我想我已经使用 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>
4

1 回答 1

0

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

  1. 实施OnMyLocationChangeListener
  2. myMap.setMyLocationEnabled(true)(启用具有内置位置提供程序的 my-location 层)
  3. myMap.setOnMyLocationChangeListener(this)(注册以在“我的位置”点更改位置时接收更新)
  4. 在您的回调方法onMyLocationChange(Location location)中相应地更新相机。

您已经拥有所有这些的代码,但我看到您已在步骤 3 中评论了该行,这可能是您看不到相机更新的原因。

您不需要实现LocationSourceand LocationListener(因为 my-location 图层有自己的位置提供程序),并且OnMapClickListener仅当您想要响应用户点击地图上的某个点时才需要。

于 2013-04-17T13:04:21.457 回答