0

我正在尝试使用latitude和查找用户位置longitude。是否可以添加一个按钮来保存location (lat, long)并跳转到另一个页面?或者也许在地图上获取用户位置的图像。请帮我看看该怎么做。谢谢。

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);
    }
}

日志猫.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
4

2 回答 2

1

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

您在第 114 行有一个 Null 点异常。您在那里分配的东西是 Null 并且没有值。

于 2013-05-09T02:25:54.277 回答
0

我会尝试将您的问题分解为更小的部分。首先,您可以将位置保存在SharedPreferencesDatabase中。您可以从链接中详细阅读。您已经在currentPosition变量中获取位置。您可以单独保存纬度和经度,也可以将它们一起保存。要在单击按钮时保存它,只需在您的 xml 文件中创建一个按钮:

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

Activity. 现在单击按钮,saveLoc将调用函数,您可以在其中保存位置。在函数外部声明变量currentPosition,以便其他函数可以访问它。至于跳转到其他页面(在 Button Click 上打开另一个活动),您可以阅读此处。所以,你可以尝试这样的事情:

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);
    }
}

要获取地图的用户位置图像,您可能需要阅读Android : how to capture a image of GPS location question。问的人正在尝试做类似的事情。

希望这可以帮助。

于 2013-05-08T04:00:15.200 回答