0

I think I have done all using the Google Maps API v2 but the position is not shown and the camera doesn't changes its position.

The map loads normally but stays only in 0,0 location and it never moves.

In the device, I see the GPS signal only looking for position and I have already tested outside.

Here is my code: 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");
}
}

Manifest:

      <?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>
Kara
  • 6,115
  • 16
  • 50
  • 57
Chaddoud
  • 1
  • 1

1 Answers1

0

If what you're trying to accomplish is to have the camera automatically update when the user (My Location dot) changes location, then you only need the following:

  1. implement OnMyLocationChangeListener
  2. myMap.setMyLocationEnabled(true) (enable the my-location layer, which has a built-in location provider)
  3. myMap.setOnMyLocationChangeListener(this) (register to receive updates when My Location dot changes location)
  4. in your callback method onMyLocationChange(Location location) update the camera accordingly.

You already have the code for all of this, but I see you have commented the line in step 3, and this is probably the reason why you don't see the camera updating.

You don't need to implement LocationSource and LocationListener (because the my-location layer has its own location provider), and you need OnMapClickListener only if you want to respond to a user tapping on a point on the map.

Nevermore
  • 995
  • 1
  • 9
  • 11