2011-12-01 3 views
0

휴대 전화의 GPS 위치를 변경하면 프로그램이 충돌하지만 Google지도 ID (ID로 찾기)를 찾지 못하는 것 같습니다 내 코드 :Android GPS,지도가 위치를 변경하지 않습니다

package Maps.GeoLocation.Google; 

import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

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 MapsGeoLocationActivity extends MapActivity { 
    MapController mControl; 
    GeoPoint GeoP; 
    MapView mapV; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     LocationListener ll = new mylocationlistener(); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 

    } 

    public void maps(double pLati, double pLongi) 
    { 
     mapV = (MapView) findViewById(R.id.mapView); 
     mapV.displayZoomControls(true); 
     mapV.setBuiltInZoomControls(true); 
     GeoP = new GeoPoint ((int) (pLati *1E6), (int) (pLongi *1E6)); 
     mControl = mapV.getController(); 
     mControl.animateTo(GeoP); 
     mControl.setZoom(13); 
    } 
    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 
} 

class mylocationlistener implements LocationListener { 
    double pLong; 
    double pLat; 

    @Override 
    public void onLocationChanged(Location location) { 
      MapsGeoLocationActivity ff = new MapsGeoLocationActivity(); 
      pLong = location.getLongitude(); 
      pLat = location.getLatitude(); 
      //textLat.setText(Double.toString(pLat)); 
      //textLong.setText(Double.toString(pLong)); 
      ff.maps(pLat, pLong); 

    } 

    @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 

    } 

} 

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" 
    > 
<com.google.android.maps.MapView 
       android:id="@+id/mapView" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:enabled="true" 
       android:clickable="true" 
       android:apiKey="apikey" 
       /> 
</LinearLayout> 

로그 캣 : 이 http://img195.imageshack.us/img195/5655/16325616.png

답변

2

좋아, 여기에 일이 : Firstl y, 어떻게 프로그램을 구조화하고 싶은지 생각해보십시오.

활동의 onCreate 메소드에 위치 수신기를 등록합니다. 그런 다음 onLocationChanged 메소드에서 동일한 작업의 새 인스턴스를 작성하여 onCreate가 다시 호출되도록합니다. 그래서 같은 청취자를 다시 등록 할 때, 그리고 위치가 몇 번이나 반복 될 때 등등. 나는 NullPointer가 어디서 왔는지 정확히 모르겠다. 그러나 나는 당신이 만든이 무한 사이클과 관련이 있다고 믿는다.

이렇게하는 것이 아닙니다. 가장 좋은 방법은 LocationListener가 서비스로 작동하도록하는 것입니다 (즉, MapView를 "호스트하는"활동과는 독립적입니다). 따라서 onLocationChanged 메소드에서보기 및 위치를 업데이트 할 활동에 인 텐트를 보내야합니다. 지도.

희망적입니다.

추신 : 나는 Android 개발에 익숙하지 않으므로 코드 작성을 시작하기 전에 http://developer.android.com/guide/topics/fundamentals.html을 완벽하게 이해했는지 확인하십시오. 또한 : http://developer.android.com/guide/topics/fundamentals/activities.html 및이 : http://developer.android.com/guide/topics/fundamentals/services.html

관련 문제