GPS coordinates not being retrieved - android

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/

Related

How to find distance and travel route from current location to set marker

I am doing project on taxi fare calculation, my current location is displayed but i want to calculate distance and also show travel route from current location to other city using latitude..
please suggest n help...
Maps.java
package com.example.mainproject;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import com.example.mainproject.R;
import com.google.android.gms.maps.CameraUpdate;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class Maps extends FragmentActivity implements LocationListener {
// Google Map
private GoogleMap googleMap;
private LocationManager locationManager;
private static final long MIN_TIME = 400;
private static final float MIN_DISTANCE = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
try {
// Loading map
initilizeMap();
// Changing map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
// Showing / hiding your current location
googleMap.setMyLocationEnabled(true);
// Enable / Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME,
MIN_DISTANCE, this); //You can also use LocationManager.GPS_PROVIDER and
LocationManager.PASSIVE_PROVIDER
}
#Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
googleMap.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
#Override
public void onProviderEnabled(String provider) { }
#Override
public void onProviderDisabled(String provider) { }
#Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
}
map.xml
<?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" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mainproject"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.mainproject.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.mainproject.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<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" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Requires OpenGL ES version 2 -->
<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" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.example.mainproject.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 class=".selectcity" android:label="selectcity"
android:name="com.example.mainproject.selectcity">
</activity>
<activity class=".About" android:label="About"
android:name="com.example.mainproject.About">
</activity>
<activity class=".entervalues" android:label="entervalues"
android:name="com.example.mainproject.entervalues">
</activity>
<activity class=".exit" android:label="exit" android:name="com.example.mainproject.exit">
</activity>
<activity class=".Maps" android:label="Maps" android:name="com.example.mainproject.Maps">
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIza***************`enter code here`"/>
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
For getting direction and distance from Google you have to do following:
Write this code on direction button's click listener or anywhere from that you want to show direction and distance.
String uri = "http://maps.google.com/maps?saddr="
+ source_latitude + "," + source_longitude + "&daddr="
+ destination_latitude + ","
+ destination_longitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);

Android google maps v2 cannot be resolved or is not a field, "name" bound to namespace

i am creating a android which shows the user location with Google Maps through gps. when i run my program i have the following errors.
activity_main cannot be resolved or is not a field, displayMap cannot be resolved or is not a field
main cannot be resolved or is not a field. at first this error did not appear but after i save or clean my project, it appears
i have change the displayMap and save, clean my project in strings and xml file. but the error is still there.
Attribute "name" bound to namespace "http://schemas.android.com/apk/res/android" was already specified for element "meta-data" in my android manifest
Parser exception for /MyAppName/AndroidManifest.xml: Attribute "name" bound to namespace "http://schemas.android.com/apk/res/android" was already specified for element "meta-data". the resource for this is my ApplicationNameApp(my project name)
does that means i must change the "name" under meta data?
strings file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">StudentHealthApp</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="displayMap">Google Map Display</string>
</resources>
XML File
<?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" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/displayMap"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Android Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studenthealthapp"
android:versionCode="1"
android:versionName="1.0" >
<permission android:name="your.application.package.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-features
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.example.studenthealthapp.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="API key value"
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
Main Java file
import android.R;
import android.location.Criteria;
import android.location.Location;
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;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if(googleMap == null)
{
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.displayMap)).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 = (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_HYBRID);
//Get latitude of the current location
double latitude = myLocation.getLatitude();
//Get longitude of the current location
double longitude = myLocation.getLongitude();
//Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
//Show the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));
}
#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;
}}
i have added google play services as a project, google-play-services_lib appear as a seperate project folder then my application in eclipse. then i add to my libarary by Project -> Properties -> Android -> Library, Add -> google-play-services_lib
Change this
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAZUoTQoHQwP25j0L_hTIUsXjxWmkFi3vg"
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
to
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAZUoTQoHQwP25j0L_hTIUsXjxWmkFi3vg"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Remove
import android.R;
Instead you need
import com.example.studenthealthapp.R;
But if its in the same package then there is no need for import.
Also if you have errors in your resources files R.java will not be generated. So if any errors fix them and clean and build project.
remove import android.R;
and import your package name.R file
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
}

location manager in Android apps

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

Android Location Services not working

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.

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">

Categories

Resources