0

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.<init>(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
XiAnG
  • 245
  • 2
  • 9
  • 25

2 Answers2

1

E/AndroidRuntime(508): Caused by: java.lang.NullPointerException 05-09 02:02:48.300: E/AndroidRuntime(508): at mp.memberuse.Map.(Map.java:114)

You have a Null point exception on line 114. something you are assigning there is Null and doesn't have a value.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • how to i get my currentPosition and add the method given by Shobhit Puri? – XiAnG May 09 '13 at 02:33
  • I can't understand your question, what is on line 114? – Emil Adz May 09 '13 at 02:37
  • `Location location;` `LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());` `String myLocation = currentPosition.toString();` – XiAnG May 09 '13 at 02:44
  • i edit my question with the method added. Line 114 : `LatLng currentPosition = new LatLng(location.getLatitude(),location.getLongitude());` – XiAnG May 09 '13 at 02:50
  • I guess that your location object is null. – Emil Adz May 09 '13 at 06:49
  • `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")); }` this method work, i can get the location. How do I get the currentPosition with my location and use it in saveLoc method? – XiAnG May 09 '13 at 07:09
  • I don't get it, if you get a location the you can create the currentPosition object, what is the problem then? – Emil Adz May 09 '13 at 07:49
  • i trying to add a button to save my location and jump to another page. but i not sure how to get my currentPosition working in the button onclick method. – XiAnG May 09 '13 at 09:12
0

I'll try and break down your question to smaller parts. Firstly, you may save the location in SharedPreferences or Database. You can read in detail from the links. You are already getting the location in currentPosition variable. You can separately save latitude and longitude or save them together. For saving it using on button click, just create a button in the xml file of yours:

<Button 
 android:id="@+id/saveBtn"
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:onClick="saveLoc"
 android:text="Save" />

Have a function called 'saveLoc' in the Activity. Now on clicking the button, saveLoc function would be called where you may save the location. Declare the variable currentPosition outside the function, so that other funciton may access it. As far as jumping to other page( opening another activity on Button Click) is concerned, you can read here. So, you can try to so something like this:

void saveLoc(){
    if ( currentPosition!=null){
    // save Location in SharedPreference or a Database here
    // Start new activity
    Intent intent = new Intent(OldActivity.this, NewActivity.class);
    startActivity(intent);
    }
}

For taking image of the user Location of the map, you may want to read Android : how to capture a image of GPS location question. The person who asked is trying to do similar thing.

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • i try adding the button with the saveloc code u gave but the apps force close straight away – XiAnG May 08 '13 at 07:17
  • Not sure what error it get. I tested it on my phone. My AVD unable to run google map on it. – XiAnG May 09 '13 at 01:02
  • You can check the error by seeing the LogCat in your eclipse. Without seeing the error how can you correct it? – Shobhit Puri May 09 '13 at 01:28