Hi the MapActivity starts and it shows a map with lat and longitude (0,0), i can't get the current position because i get this error:
09-16 12:06:46.515: ERROR/MapActivity(464): Couldn't get connection factory client
The apikey is correct, i've also reinstalled eclipse + android sdk and regenerate another debug.keystore and its relative apikey, but nothing changed..
The source is this (note that i dont see the println on logcat System.out.println("* i'm here"+current_lat));
:
package it.me.map;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;
import it.man.app.R;
public class Maps extends MapActivity
{
private TextView tvName;
private TextView tvAddress;
private TextView tvOpenTime;
private MapController mc;
private MapView mapView;
private static double current_lat;
private static double current_long;
private static LocationListener mNetworkListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
makeUseOfNewLocation(location);
}
开发者_StackOverflow社区
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void makeUseOfNewLocation(Location location) {
current_lat = location.getLatitude();
current_long = location.getLongitude();
System.out.println("*** i'm here"+current_lat);
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
mapView = (MapView) findViewById(R.id.mapview);
mc = mapView.getController();
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mNetworkListener);
GeoPoint p = new GeoPoint((int)current_lat*(1000000), (int)current_long*(1000000));
mc.animateTo(p);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
the maps.xml it's this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:apiKey="02pmISdykDhJpseMz_d3XLv5ojPsNW25Tz9dtdA"
/>
</RelativeLayout>
and finally the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.marco.furesta"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Maps"></activity>
<activity android:name=".NewsList"></activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
I think you forget to specify the api key in manifest and just add this permission
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
add api key like this.
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Your api key"/>
This code worked for me.
public class LocTrack extends Service implements LocationListener {
LocationManager lm;
public double lat, lon;
public boolean InitGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(LocTrack.this, "Please Enable GPS!", Toast.LENGTH_LONG).show();
Intent gps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gps);
return false;
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, this);
return true;
}
public void StopGPS() {
lm.removeUpdates(this);
}
@Override
public void onLocationChanged(Location l) {
Storage.lat = l.getLatitude();
Storage.lon = l.getLongitude();
Storage.changed = true;
Toast.makeText(LocTrack.this,"location",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
InitGPS();
}
@Override
public void onProviderDisabled(String provider) {
StopGPS();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
InitGPS();
return START_STICKY;
}
}
I used this fragment for displaying map.
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
精彩评论