Thursday, May 9, 2013

[android help] getting user location with a button click on google map


I am trying to find the user location with latitude and longitude. Is it possible to add a button to save the location (lat, long) and jump into another page? Or maybe get the image of the user location on the map. Do help me out on what to do. Thanks.



package mp.memberuse;

import android.app.Dialog;
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 com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Map extends FragmentActivity {

GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);

// 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
{ // Google Play Services are available

// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

// Getting GoogleMap object from the fragment
googleMap = fm.getMap();

// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);

// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();

// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);

// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);

LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
// redraw the marker when get location update.
drawMarker(location);
}

@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

}
};

if(location!=null)
{
//PLACE THE INITIAL MARKER
drawMarker(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
}
}
private void drawMarker(Location location)
{
googleMap.clear();
LatLng currentPosition = new LatLng(location.getLatitude(),
location.getLongitude());
googleMap.addMarker(new MarkerOptions().position(currentPosition).snippet("Lat:" + location.getLatitude() + "Lng:"+ location.getLongitude()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("ME"));
}
Location location;
LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());
String myLocation = currentPosition.toString();

void saveLoc(){
if ( myLocation!=null){
// save Location in SharedPreference or a Database here
SharedPreferences prefs = getSharedPreferences("myPreferences",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("myLocation", myLocation);
editor.commit();
// Start new activity
Intent intent = new Intent(Map.this, SendMessage.class);
startActivity(intent);
}
}


Logcat.txt



05-09 02:01:54.280: D/dalvikvm(508): GC_EXTERNAL_ALLOC freed 51K, 53% free 2576K/5379K, external 3129K/3266K, paused 72ms
05-09 02:01:57.891: W/KeyCharacterMap(508): No keyboard for id 0
05-09 02:01:57.891: W/KeyCharacterMap(508): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-09 02:02:34.450: W/KeyCharacterMap(508): No keyboard for id 0
05-09 02:02:34.450: W/KeyCharacterMap(508): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-09 02:02:34.490: D/dalvikvm(508): GC_CONCURRENT freed 1486K, 58% free 3019K/7111K, external 3430K/4246K, paused 6ms+4ms
05-09 02:02:46.381: I/dalvikvm(508): Total arena pages for JIT: 11
05-09 02:02:47.931: D/dalvikvm(508): GC_CONCURRENT freed 1315K, 59% free 2950K/7111K, external 3430K/4246K, paused 5ms+4ms
05-09 02:02:48.281: D/AndroidRuntime(508): Shutting down VM
05-09 02:02:48.281: W/dalvikvm(508): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-09 02:02:48.300: E/AndroidRuntime(508): FATAL EXCEPTION: main
05-09 02:02:48.300: E/AndroidRuntime(508): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{mp.memberuse/mp.memberuse.Map}: java.lang.NullPointerException
05-09 02:02:48.300: E/AndroidRuntime(508): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
05-09 02:02:48.300: E/AndroidRuntime(508): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-09 02:02:48.300: E/AndroidRuntime(508): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-09 02:02:48.300: E/AndroidRuntime(508): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-09 02:02:48.300: E/AndroidRuntime(508): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 02:02:48.300: E/AndroidRuntime(508): at android.os.Looper.loop(Looper.java:123)
05-09 02:02:48.300: E/AndroidRuntime(508): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-09 02:02:48.300: E/AndroidRuntime(508): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 02:02:48.300: E/AndroidRuntime(508): at java.lang.reflect.Method.invoke(Method.java:507)
05-09 02:02:48.300: E/AndroidRuntime(508): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-09 02:02:48.300: E/AndroidRuntime(508): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-09 02:02:48.300: E/AndroidRuntime(508): at dalvik.system.NativeStart.main(Native Method)
05-09 02:02:48.300: E/AndroidRuntime(508): Caused by: java.lang.NullPointerException
05-09 02:02:48.300: E/AndroidRuntime(508): at mp.memberuse.Map.(Map.java:114)
05-09 02:02:48.300: E/AndroidRuntime(508): at java.lang.Class.newInstanceImpl(Native Method)
05-09 02:02:48.300: E/AndroidRuntime(508): at java.lang.Class.newInstance(Class.java:1409)
05-09 02:02:48.300: E/AndroidRuntime(508): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-09 02:02:48.300: E/AndroidRuntime(508): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
05-09 02:02:48.300: E/AndroidRuntime(508): ... 11 more
05-09 02:02:55.531: I/Process(508): Sending signal. PID: 508 SIG: 9


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...