纬度和经度值给出null

时间:2014-10-08 06:27:47

标签: java android eclipse android-emulator

我是android编程和堆栈溢出的新手。我的问题在于onclick方法的mylongitudey给出null值。我有3个活动。其中一个是其中之一。我想要谷歌地图的纬度和经度,并将这些点发送到另一个活动。这里的主要问题是,它没有给出纬度和经度值而是给出了空值。我无法找到错误。谁能帮我?谢谢你提前。

SharedPreferences points;
SharedPreferences.Editor ed;

Address returnedAddress;

double mylongitudey,mylatitudey;
String latilongi;
ImageView iv;
Button manual,proceed;

GoogleMap map;
LatLng mylat ;

String latlong;
Marker mymarker;
boolean markerclicked;
double mylatitudeyd,mylongitudeyd;
String addstringaa;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.two_buttons);
  actionBar = getActionBar();
  actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLACK));
  getActionBar().setDisplayHomeAsUpEnabled(true);


  Toast.makeText(this,"Please select the destination by long click", Toast.LENGTH_LONG).show();

  LatLng mylat;
  final GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
  map.setOnMarkerDragListener(this);
  map.setTrafficEnabled(true);
 // map.setOnMyLocationButtonClickListener(this);

  SharedPreferences shared = getSharedPreferences("map", 0);
  String mylats = (shared.getString("twolati", "NoValue"));
  String mylong= (shared.getString("twolong", "NoValue"));
  double dmylats = Double.valueOf(mylats);
  double dmylong = Double.valueOf(mylong);
   map.animateCamera(CameraUpdateFactory.zoomTo(15));
  mylat = new LatLng(dmylats, dmylong);
  try {
  Toast.makeText(this, "This is"+mylats,Toast.LENGTH_LONG).show();
} catch (Exception e) {
    Toast.makeText(this,"problem in mylat",0);
    e.printStackTrace();
}


 map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylat, 13));
 map.addMarker(new MarkerOptions().position(mylat).title("You are Here"));

 markerclicked=false;
  //onMyLocationButtonClick();
  map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

    @Override
    public void onMapLongClick(LatLng arg0) {
        // TODO Auto-generated method stub


        if(markerclicked==false)
        {
         mymarker = map.addMarker(new MarkerOptions().position(arg0).title("Your Destination").draggable(true));
         markerclicked=true;
    }else{

         Toast.makeText(getApplicationContext(), "Marker Aready Set..!", Toast.LENGTH_LONG).show();

    }

    }
});


  map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
    LatLng mylatlong;
    @Override
    public void onMyLocationChange(Location arg0) {

        // TODO Auto-generated method stub
        mylongitudey = arg0.getLongitude();
        mylatitudey = arg0.getLatitude();
        mylatlong = new LatLng(arg0.getLatitude(), arg0.getLongitude());
        //sourcepoint=String.format(" ",mylat);

//  map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylat,1));
         map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylatlong, 14));


    }
});


 //   Toast.makeText(this, getmapid, Toast.LENGTH_LONG).show();

SharedPreferences pr= getSharedPreferences("reserve", 0);
latilongi=pr.getString("sourcepoint", "sourcepoint");

manual=(Button)findViewById(R.id.manuel);
manual.setOnClickListener(this);

proceed=(Button)findViewById(R.id.proceed);
proceed.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {



    getMenuInflater().inflate(R.menu.actionmenu, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Take appropriate action for each action item click
    // Toast.makeText(MainActivity.this, item.getTitle(),
    // Toast.LENGTH_LONG).show();


    switch (item.getItemId()) {

    case android.R.id.home:
        if (getParentActivityIntent() == null) {
            Log.i("tag", "You have forgotten to specify the parentActivityName in the     AndroidManifest!");
            onBackPressed();
        } else {
            NavUtils.navigateUpFromSameTask(this);
        }
        return true;

    case R.id.action_refresh:
        // refresh

        Intent intent = new Intent(Drop_Off_Location.this, Main.class);
        startActivityForResult(intent, 0);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}



public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onClick(View v) {



        try {
             if (v.getId() == R.id.proceed) {
                /* Toast.makeText(this, "address"+mylongitudey, 0);
                 Toast.makeText(this, " destination in DOL class "+addstringaa, 0).show();*/
                 points= getSharedPreferences("map", 0);
                 ed=points.edit();
                 String twolatitudey = String.valueOf(mylatitudey);
                 String twolongitudey = String.valueOf(mylongitudey);                                        
                 ed.putString("twolatitudey",twolatitudey);
                 ed.putString("twolongitudey", twolongitudey);                   
                 ed.commit();
                 Toast.makeText(this, "dest longitude"+mylongitudey, Toast.LENGTH_LONG).show();
                 Intent intent = new Intent(Drop_Off_Location.this,Autofill.class);
                 startActivity(intent); 

            }
             else if(v.getId()==R.id.manuel)
             {
                 Intent intent = new Intent(Drop_Off_Location.this,Reserve.class);
                    startActivity(intent);                   
             }
        } catch (Exception e) {
             Toast.makeText(this,"error in onclick"+e, Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
}  

Logcat详细:

10-08 02:23:53.965: D/dalvikvm(3221): GC_FOR_ALLOC freed 111K, 25% free 7167K/9528K, paused 132ms, total 134ms
10-08 02:23:53.975: I/dalvikvm-heap(3221): Grow heap (frag case) to 9.411MB for 2457616-byte allocation
10-08 02:23:54.145: D/dalvikvm(3221): GC_FOR_ALLOC freed 2K, 20% free 9565K/11932K, paused 167ms, total 167ms
10-08 02:23:55.895: W/System.err(3221): java.io.IOException: Service not Available
10-08 02:23:55.895: W/System.err(3221):     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
10-08 02:23:55.905: W/System.err(3221):     at jasperit.atlcitylimo.Autofill.onCreate(Autofill.java:189)
10-08 02:23:55.905: W/System.err(3221):     at android.app.Activity.performCreate(Activity.java:5231)
10-08 02:23:55.905: W/System.err(3221):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 02:23:55.905: W/System.err(3221):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-08 02:23:55.905: W/System.err(3221):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-08 02:23:55.905: W/System.err(3221):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-08 02:23:55.905: W/System.err(3221):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-08 02:23:55.905: W/System.err(3221):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-08 02:23:55.905: W/System.err(3221):     at android.os.Looper.loop(Looper.java:136)
10-08 02:23:55.905: W/System.err(3221):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-08 02:23:55.905: W/System.err(3221):     at java.lang.reflect.Method.invokeNative(Native Method)
10-08 02:23:55.915: W/System.err(3221):     at java.lang.reflect.Method.invoke(Method.java:515)
10-08 02:23:55.915: W/System.err(3221):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-08 02:23:55.915: W/System.err(3221):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-08 02:23:55.915: W/System.err(3221):     at dalvik.system.NativeStart.main(Native Method)
10-08 02:23:56.015: W/System.err(3221): java.io.IOException: Service not Available
10-08 02:23:56.015: W/System.err(3221):     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
10-08 02:23:56.035: W/System.err(3221):     at jasperit.atlcitylimo.Autofill.onCreate(Autofill.java:227)
10-08 02:23:56.035: W/System.err(3221):     at android.app.Activity.performCreate(Activity.java:5231)
10-08 02:23:56.035: W/System.err(3221):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 02:23:56.045: W/System.err(3221):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-08 02:23:56.045: W/System.err(3221):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-08 02:23:56.045: W/System.err(3221):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-08 02:23:56.045: W/System.err(3221):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-08 02:23:56.045: W/System.err(3221):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-08 02:23:56.045: W/System.err(3221):     at android.os.Looper.loop(Looper.java:136)
10-08 02:23:56.085: W/System.err(3221):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-08 02:23:56.085: W/System.err(3221):     at java.lang.reflect.Method.invokeNative(Native Method)
10-08 02:23:56.085: W/System.err(3221):     at java.lang.reflect.Method.invoke(Method.java:515)
10-08 02:23:56.085: W/System.err(3221):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-08 02:23:56.095: W/System.err(3221):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-08 02:23:56.095: W/System.err(3221):     at dalvik.system.NativeStart.main(Native Method)
10-08 02:23:56.255: I/Choreographer(3221): Skipped 688 frames!  The application may be doing too much work on its main thread.
10-08 02:23:57.585: I/Choreographer(3221): Skipped 267 frames!  The application may be doing too much work on its main thread.
10-08 02:23:58.885: I/Choreographer(3221): Skipped 114 frames!  The application may be doing too much work on its main thread.
10-08 02:23:59.675: I/Choreographer(3221): Skipped 107 frames!  The application may be doing too much work on its main thread.

2 个答案:

答案 0 :(得分:1)

//instance variables
 private GoogleMap googleMap;
 private double longitude = 0.0;
 private double latitude = 0.0;
 private LocationManager locationManager = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);   // your layout
  try {
    initilizeMap();     

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, this);

    googleMap.setMyLocationEnabled(true);   
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

   }
 } catch (Exception e) {
        e.printStackTrace();
 }
 }


 private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();    //provide your id       

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}

    @Override
    public void onLocationChanged(Location location) {
       // TODO Auto-generated method stub
       longitude = location.getLongitude();
      latitude = location.getLatitude();
      //System.out.println(latitude+ " "+longitude );
      if(!isStopFocusEnabled){
          CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(latitude, longitude)).zoom(12).build(); 
          googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
       }


}

在Manifest下面的代码必须在那里

      <permission
    android:name="com.ani.map.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.ani.map.permission.MAPS_RECEIVE" />

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.ACCESS_GPS" />
<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" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />



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


    <!-- Goolge API Key -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YOUR KEY" />

    <meta-data 
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>

//相应地改变它......它肯定会起作用......

答案 1 :(得分:-1)

public boolean onTouchEvent(MotionEvent event)
{
    int X = (int)event.getX();          
    int Y = (int)event.getY();

    GeoPoint geoPoint = mapView.getProjection().fromPixels(X, Y);
}