location manager in Android apps - android

I create simple android apps which show location of device but when I run in emulator it show error in run the apps.
package my.loc;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class LocActivity extends Activity {
/** Called when the activity is first created. */
TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
textView = (TextView) findViewById(R.id.text_view);
LocationManager manager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Location loc =
manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
textView.setText("latitude: " + loc.getLatitude()
+ "\nlongitude: " + loc.getLongitude());
} }
Activity.xml file is such As
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text_view"
/>
and manifest is such as
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.loc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".LocActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Error is:
The application loc has stopped unexpected.

You must have your class implement LocationListener and call manager.requestLocationUpdates() in order to obtain location information. See further details at http://developer.android.com/training/location/receive-location-updates.html

Related

Google Maps Android API V2 showing current location with marker(where am i in google android maps api v2)

I am beginner in android , i am developing an android app for displaying my current location on google maps.But as i run my application it display a message "Unfortunately stopped" on emulator as well as physical device.
i am attaching my code .plz have a look.
pls help me ....i am not able to resolve this
thanks
MainActivity.java
package com.example.mapp;
import android.app.Dialog;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends FragmentActivity implements OnMyLocationChangeListener {
GoogleMap googlemap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int status=GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(status!=ConnectionResult.SUCCESS)
{
int requestCode=10;
Dialog dialog=GooglePlayServicesUtil.getErrorDialog(status,this, requestCode);
dialog.show();
}
else
{
SupportMapFragment fm = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
googlemap=fm.getMap();
googlemap.setMyLocationEnabled(true);
googlemap.setOnMyLocationChangeListener(this);
}
}
#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;
}
#Override
public void onMyLocationChange(Location location) {
TextView tvLocation=(TextView)findViewById(R.id.textview1);
double latitude=location.getLatitude();
double longitude=location.getLongitude();
LatLng latLng=new LatLng(latitude,longitude);
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(15));
tvLocation.setText("Latitude:"+latitude+",Longitude:"+longitude);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<permission android:name="com.example.mapp.permission.MAP_RECIEVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapp.permission.MAP_RECIEVE"/>
<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" />
<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" >
<activity
android:name="com.example.mapp.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.maps.v2.API_KEY"
android:value="MY_API_KEY" />
</application>
</manifest>
*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" >
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/textview1"
/>
</RelativeLayout>
Add the following code inside application tag
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
I know to show your current location, you must use a real device

Android MapView Displays Black Screen

i am using google maps in my app. i am unable to get the map tiles. i just see a black screen. i have been struggling for 3 days now. i generate the map api key on my computer and i have added all the permissions in my menifest.xml file. but still unable to get map tiles.
ActivityMap.java
`
package com.example.locatometer;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import com.example.locatometer.utils.Tracker;
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 ActivityMap extends MapActivity{
MapView mapView;
MapController controller;
Tracker tracker;
Handler locationHandler = new Handler();
GeoPoint mapPoint;
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_current_location_on_map);
init();
}
private void init() {
mapView = (MapView) findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
controller = mapView.getController();
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
`
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/map"
android:clickable="true"
android:apiKey="******************************" />
</LinearLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locatometer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_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/LocatometerTheme" android:debuggable="true">
<uses-library android:name="com.google.android.maps"/>
<activity
android:name="com.example.locatometer.ActivitySplash"
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="com.example.locatometer.ActivityMenu"></activity>
<activity android:name="com.example.locatometer.ActivityCurrentLocation"></activity>
<activity android:name="com.example.locatometer.ActivityMap"></activity>
</application>
</manifest>
It might not be related but i did this once when i was faced with a similar kind of issue. Under your application tag in manifest put
android:hardwareAccelerated="false"
Might not work but worth a try.

error application when show map

I'm new to android
I'm trying make a simple app in android with google map, but when I run it in emulator look error
The Application MapGoogle (process com.jol.android.Mapgoogle) has stopped unexpectedly. Please try again.
This is my GoogleMap.java
package com.jol.android.Mapgoogle;
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 Googlemaps 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.mapview);
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, 0, 0, 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) {
}
}
}
and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jol.android.Mapgoogle"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name=".mapgoogle"
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" />
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
This my main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:clickable="true"
android:apiKey="0Qe1BE05sZZFeWkfqemBVn-tw_Y_Kc9E40HpY-w" />
</RelativeLayout>
But when I run this program I got this error
The Application MapGoogle (process com.jol.android.Mapgoogle) has stopped unexpectedly. Please try again.
Why like this? Please help me. Thank you, mate.
Try this
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
mobiForge: Using Google Maps in Android
You have several problems, the main one being the line:
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mapview);
It's an illegal cast and it does nothing - get rid of it!
As a matter of style your package should be all lower case, make it
package com.jol.android.mapgoogle;
Make sure you compilation unit is called Googlemaps.java and that it matches the class name.
As other posters have pointed out, you need certain permissions in the manifest. This manifest should work and match the other changes I have mentioned.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jol.android.mapgoogle"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".Googlemaps"
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:name="com.google.android.maps"
android:required="true" >
</uses-library>
</application>
</manifest>
.
You need to add following tag in your manifest file
<application >
<activity>
<uses-library android:name="com.google.android.maps" />
</activity>
</application>
Put this in the manifest:
<uses-permission android:name="android.permission.INTERNET">

Android SecurityException ACCESS_WIFI_STATE

I am building an Android app that uses wifi.
I have properly declared these uses permissions in the manifest, however for some reason the app is throwing a SecurityException that causes the app to force-close.
I traced the cause of the security exception in LogCat to the system saying it doesn't have the access_wifi_state permission. This is strange and confusing to me because I have already declared it in the manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.wifi"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="ACCESS_WIFI_STATE" />
<uses-permission android:name="CHANGE_WIFI_STATE" />
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="android.wifi.AndroidWiFiActivity"
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>
And here is the code that causes the exception:
package android.wifi;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.wifi.R;
public class AndroidWiFiActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textbox);
tv.setText("ABC 123");
WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifiMgr.isWifiEnabled())
{
tv.append("Wifi is enabled");
}
else
{
tv.append("Wifi is disabled");
}
}
}
Any help is greatly appreciated. Thanks.
The permission is:
android.permission.ACCESS_WIFI_STATE

GPS coordinates not being retrieved

HI i have been trying to run this code from an example in a book but all i get is is the null value being passed to the variable and so i only get the message as "Your Current Position is : no location found"
The manifest file is as below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.snooze.android.geopositioning"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_INTERNET" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<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>
</application>
</manifest>
The main.xml file is
<?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/myLocationText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
Lastly is the MainActivity.java
package com.snooze.android.geopositioning;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
}
public void updateWithNewLocation(Location location)
{
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + String.valueOf(lat) + "\nLong:" + String.valueOf(lng);
}
else
{
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" + latLongString);
}
}
This is my first project so i am unfamiliar with a few of the workings but i copied everything as the book said but it does not work. Have tried various things on many sites, as well as answers from this forum....but to no avail.
I am using Eclipse and have added tried to run it on and AVD with Android 2.2 as well as for Google APIs.
What i think is that the co-ordinates are not being passed to the variables.
I had posted as a new user but was not able to comment on it so i have asked it again.
Lukas Kunth, chaitanya and wareninja thanks for your answers.
Apologies for the duplication.
Please help
You are missing the LocationListener without which your application cannot receive location updates.
Here is an excellent blog on GPS Location fetching..
http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/

Categories

Resources