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