I search a lot and also see a lot of post inside stackoverflow but no answered or it was not clear and complicated. it's simple i want to onLocationChanged() method called when i send Longitude and Latitude from emulator control so i wrote this code inside main.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.mrg.findlocation.Main$PlaceholderFragment" >
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
And put this code inside Main.java file:
package com.mrg.findlocation;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView text = (TextView)findViewById(R.id.textview);
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListner = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double latti = location.getLatitude();
double longi = location.getLongitude();
Log.d("MRG","It worked");
text.setText("latti :"+latti+"\n"+"longi :"+longi);
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListner);
}
}
And inside manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mrg.findlocation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.mrg.findlocation.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Then i send two value from emulator control nothing happened:
Are there anyone to solve this problem one time for ever? the code i pasted here it's very simple to reproduce and it doesn't contain google map or anotherthing
Edit:
This code is suitable for real device
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListner);
This code works on emulator
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListner);
I think the problem is that you're requesting updates from the Network Provider and the emulator uses GPS provider.
Try changing:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListner);
with:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListner);
Try to implements LocationListener on your activity class
public class Main extends Activity implements LocationListener {
#Override
public void onLocationChange(Location location){
// do something
}
}
Related
I am trying to write a data sharing application which will run 24/7 and keep on sharing data whenever user travels more than 650 meters in 3 minutes.
Error is that the app stops unexpectedly if I pass any non-zero value lets say 18000 in minTime rather than passing 0. Works absolutely fine if minTime = 0.
Here is my code..
MainActivity.java
package com.example.agentbackgroundbroadcastreciever;
import android.location.LocationManager;
import android.location.Criteria;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = new Intent(MainActivity.this, LocationReceiver.class);
LocationManager manager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
long minTime = 18000;
float minDistance = 650;
String provider = LocationManager.GPS_PROVIDER; //GPS_PROVIDER or NETWORK_PROVIDER
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
PendingIntent launchIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
manager.requestLocationUpdates(provider, 0, minDistance, launchIntent); ////Crashes if I put 18000 instead of 0 in minTime here
// Toast.makeText(MainActivity.this, "Came here", Toast.LENGTH_LONG).show();
}
}
LocationReceiver.java
package com.example.agentbackgroundbroadcastreciever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class LocationReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent != null)
{
Bundle bundle = intent.getExtras();
Location location = (Location) bundle.get(LocationManager.KEY_LOCATION_CHANGED);
Toast.makeText(context, "Received"+location.getLatitude()+" "+location.getLongitude(), Toast.LENGTH_LONG).show();
// DataSharing data=new DataSharing();
// data.sendSms(location.getLatitude(), location.getLongitude(), new java.util.Date());
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agentbackgroundbroadcastreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="LocationReceiver">
</receiver>
<activity
android:name="com.example.agentbackgroundbroadcastreciever.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>
</application>
</manifest>
I could not figure out what is going wrong. Help will be appreciated. Thank You.
if (LocationManager.KEY_LOCATION_CHANGED)) {
}
instead of "if(intent != null)"
when i run the application it load the map but not my current location.when i clicked on gps after then it zoom in to my current location. i also want to update the location as the user moves from one location to another. this location update code is not working properly. And when i change the orientation of device it reload the map again.Same things happens again.
Xml file
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
class file
package com.design.googlemap;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends FragmentActivity{
private GoogleMap googlemap;
private LocationManager locationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SetMapIfNeeded();
}
private void SetMapIfNeeded() {
// TODO Auto-generated method stub
if(googlemap==null){
//try to obtain google map form SupportMapFragment;
googlemap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if(googlemap!= null){
SetUpMap();
}
}
}
private void SetUpMap() {
// Enable MyLocation Layer of Google Map
googlemap.setMyLocationEnabled(true);
// Get LocationManager object from System Service Location_SERVICE
locationManager =(LocationManager) getSystemService(LOCATION_SERVICE);
//Create a criteria object to retrieve provider
Criteria criteria= new Criteria();
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria,true);
//Get Current Location
Location myLocation= locationManager.getLastKnownLocation(provider);
//Set map type
googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Get Latitude object of current location
double latitude=myLocation.getLatitude();
//Get Latitude object of current location
double longitude=myLocation.getLatitude();
//Create a LatLng object for current location
LatLng latLng = new LatLng(latitude,longitude);
//show the current location of Google Map
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
LocationListener listner = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
locationManager =(LocationManager) getSystemService(LOCATION_SERVICE);
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) listner);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ManifestFile
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.design.googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<permission
android:name="com.design.googlemap.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The following two permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.design.googlemap.permission.MAPS_RECEIVE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.design.googlemap.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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="XXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
</application>
</manifest>
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria,false);
Even if Gps if Off. Your approx location will be pointed.
And for locationUpdates on your GoogleMaps go through this link
Also for zooming to a particular position add this code:
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(YOUR_LATLNG).zoom(ZOOM_LEVEL).build(); // int ZOOM_LEVEL=12
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
go through this link for more on zooming.
To hide the WebView call the Visibility functions in your Activity[OnClick of your Button]
WebView wv=(WebView)findViewById(R.id.YOUR_WEB_VIEW);
wv.setVisibility(View.GONE);// when you want to hide[shrink]
wv.setVisibility(View.VISIBLE);// when you want to Show[Zoom]
1. If you only want to show your own location, than a MyLocationOverlay is probably easiest.
Quick information here (rest of that article might be helpful too):
http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation
Use MyLocationOverlay, as that is what it is there for. Here is a sample application that uses MyLocationOverlay and a custom overlay; you can always get rid of the custom one if you do not need it.
2. To avoid the common behavior of reloading, you have to handle configuration change for your app .
Add this line to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.
android:configChanges="keyboardHidden|orientation|screenSize"
Thanks in advance, I'm having dificulties trying to display an icon on my current location in a mobile application using google maps. At the moment I'm able to retrieve the maps from Google maps android api v2 libraries using the using API key etc., and I am able to retrieve my own location on GPS coordinates, "latitude" and "longitude" as they can be displayed on the screen in two textViews in my mobile device. My problem now is how to use those coordinates to display with an icon my own location on the map. Obviously I'm doing something wrong when I try to implement it. I tried to implement it using overlays but I'm having plenty of errors. I don't know why or what way to take.
Here is my code. Any help I would really appreciate it.
thanks
This is my MainActivity.java
package com.example.location3;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
TextView textLat;
TextView textLong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create reference
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
// create locationManager object
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// create LocationListener object (listen for changes in the location)
LocationListener ll = new myLocationListener();
// use lm location manager to get update
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
// create a LocationListener to do what we want
class myLocationListener implements LocationListener {
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// implemented method
#Override
public void onLocationChanged(Location location) {
if (location != null) {
// variables to get and store longitude and latitude
double pLong = location.getLongitude();
double pLat = location.getLatitude();
// set text to display values from longitude and latitude
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
}
}
// implemented method
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
// implemented method
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
// implemented method
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
This is my Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitud"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textLat"
android:text="Longitud"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
And this is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.location3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.example.location3.permissions.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.location3.permission.MAP_RECEIVE" />
<!-- to be able to use it for mobiles -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Permision to use Internet to get the maps -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permision for the cache which is done in the external memory -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Permision to use google services -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Permision to be able to point in the map where the user is -->
<!-- Permision to use wifi net -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Permision to use GPS -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library
android:required="true"
android:name="com.google.android.maps" />
<activity
android:name="com.example.location3.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>
<!-- API provided from Google -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCx00iNUdhMi9uyuCO9tTPwX4-nbi4wg6wx" />
</application>
</manifest>
you need to create a class which will handle the overlay wherever you want to pinup on the map. refer this tutorial here
try this :
class CurrentLocationOverlay extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
public CurrentLocationOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CurrentLocationOverlay(Drawable defaultMarker , Context context) {
this(boundCenterBottom(defaultMarker));
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
public void addOverlay(GeoPoint gp , String title , String msg) {
OverlayItem overlay = new OverlayItem(gp, title, msg);
mapOverlays.add(overlay);
this.populate();
}
}
add icon :
Drawable mDrawable = getResources().getDrawable(R.drawable.pinpoint);
GeoPoint point = new GeoPoint((int)(lat* 1E6) , (int)(lng* 1E6));
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(mDrawable);
currentLocationOverlay.addOverlay(point ,"" , "");
mapView.getOverlays().add(currentLocationOverlay);
Here goes: I've spent all night scouring the internet, especially StackOverflow, trying to figure out why I can't get basic Location Services working in Android. I have tried in a variety of emulated environments, from Android 1.6 up to Android 4, all with GPS services emulated, as well as on an actual Android 2.2.2 Device. Location is always returned as null, I've tried at least seven different downloadable sample projects, using them verbatim, and getting the same results.
Obviously, I'm missing something key. If anyone could point out what I'm doing wrong, it would be greatly appreciated.
com.mytestproject.android:
package com.mytestproject.android;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.mytestproject.android.R;
public class MyTestProject extends Activity {
private TextView mytext;
private LocationManager locmgr = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mytext = (TextView) findViewById(R.b.mytext);
//grab the location manager service
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mytext.setText("waiting for location");
}
//Start a location listener
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();
mytext.setText(latlong);
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
//pauses listener while app is inactive
#Override
public void onPause() {
super.onPause();
locmgr.removeUpdates(onLocationChange);
}
//reactivates listener when app is resumed
#Override
public void onResume() {
super.onResume();
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/mytext"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.johnandbrian.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"></uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MyTestProject"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
add this to onCreate() function,
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,10000.0f,onLocationChange);
If onLocationChanged() isn't even being called, even with sample projects, then I don't think "location is always null" is an accurate description of the problem. onLocationChanged() is only called when the location changes. So it sounds like you are (a) not injecting mock locations or otherwise driving the location engine when using the emulator, and (b) not moving around enough on the real device. I suggest loading a sample project onto the real device, going outside, and moving around some (several 10s of meters). Put some breadcrumbs in the onLocationChanged() method (in the sample project) to verify that it's being called.
I have one google maps application it does not show the maps it only shows the lines
It does not show the mapview,and i also have the internet Connection
And it also genrate the following error
09-30 12:01:12.934: WARN/Resources(587): Converting to string:
TypedValue{t=0x12/d=0x0 a=2 r=0x7f050002}
09-30 12:01:13.094: WARN/GpsLocationProvider(59): Duplicate add listener for uid
10042
09-30 12:01:13.334: INFO/MapActivity(587): Handling network change
notification:CONNECTED
09-30 12:01:13.334: ERROR/MapActivity(587): Couldn't get connection factory client
Showmap.java
package de.vogella.android.locationapi.maps;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.RelativeLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class ShowMap extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// create a map view
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 13,
14, new GeoUpdateHandler());
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point); // mapController.setCenter(point);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
This is My mainfest
?xml version="1.0" encoding="utf-8"?>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".ShowMap"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:required="true" android:name="com.google.android.maps"></uses-library>
And main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0cHF-o_cvW1DEz3ocWAyybGqUsg8aUlwKMoyr4A"
/>
Do you have the required permissions in place in the AndroidManifest.xml? In an Android app that I've made for GPS + Maps I have the following:
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
This is a problem with an API-Key.
Here you can do it.
You need to generate different one for each emulator, device etc. and for final release, and you need to merge them with an keystore you're using, what sux, but there is no different way :/.
Good luck!