2011-09-07 9 views
0

GPS를 사용하여 Google지도에서 내 위치를 오버레이하는 데이 코드를 사용했습니다. Android 3.1을 사용할 때이 코드가 작동했지만 운영체제가 3.2로 등급이 올라가면 워킹이 중지되고 마지막에 강제 종료 오류가 발생합니다 .Google지도 오버레이

이 파일은 로그 캣 출력 오차가

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
    android:id="@+id/myLocationText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
    <com.google.android.maps.MapView 
    android:id="@+id/myMapView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:enabled="true" 
    android:clickable="true" 
    android:apiKey="0LSm5iUqsTW3O-7e74GQTt4pQKnxW7YVu-3Cftg" 
    /> 
</LinearLayout> 

이 XML 주된 파일

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 

import android.content.Context; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

public class WhereAmI extends MapActivity { 
    @Override 
    protected boolean isRouteDisplayed() { 
    return false; 
    } 

    MapController mapController; 
    MyPositionOverlay positionOverlay; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    MapView myMapView = (MapView)findViewById(R.id.myMapView); 
    mapController = myMapView.getController(); 

    myMapView.setSatellite(true); 
    myMapView.setStreetView(true); 
    myMapView.displayZoomControls(false); 

    mapController.setZoom(17); 

    // Add the MyPositionOverlay 
    positionOverlay = new MyPositionOverlay(); 
    List<Overlay> overlays = myMapView.getOverlays(); 
    overlays.add(positionOverlay); 

    LocationManager locationManager; 
    String context = Context.LOCATION_SERVICE; 
    locationManager = (LocationManager)getSystemService(context); 

    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    criteria.setAltitudeRequired(false); 
    criteria.setBearingRequired(false); 
    criteria.setCostAllowed(true); 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    String provider = locationManager.getBestProvider(criteria, true); 

    Location location = locationManager.getLastKnownLocation(provider); 

    updateWithNewLocation(location); 

    locationManager.requestLocationUpdates(provider, 2000, 10, 
              locationListener); 
    } 

    private final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     updateWithNewLocation(location); 
    } 

    public void onProviderDisabled(String provider){ 
     updateWithNewLocation(null); 
    } 

    public void onProviderEnabled(String provider){ } 
    public void onStatusChanged(String provider, int status, 
           Bundle extras){ } 
    }; 

    private void updateWithNewLocation(Location location) { 
    String latLongString; 
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.myLocationText); 
    String addressString = "No address found"; 

    if (location != null) { 
     // Update my location marker 
     positionOverlay.setLocation(location); 

     // Update the map location. 
     Double geoLat = location.getLatitude()*1E6; 
     Double geoLng = location.getLongitude()*1E6; 
     GeoPoint point = new GeoPoint(geoLat.intValue(), 
            geoLng.intValue()); 

     mapController.animateTo(point); 

     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     latLongString = "Lat:" + lat + "\nLong:" + lng; 

     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 

     Geocoder gc = new Geocoder(this, Locale.getDefault()); 
     try { 
     List<Address> addresses = gc.getFromLocation(latitude, 
                longitude, 1); 
     StringBuilder sb = new StringBuilder(); 
     if (addresses.size() > 0) { 
      Address address = addresses.get(0); 

      for (int i = 0; i < address.getMaxAddressLineIndex(); i++) 
      sb.append(address.getAddressLine(i)).append("\n"); 

      sb.append(address.getLocality()).append("\n"); 
      sb.append(address.getPostalCode()).append("\n"); 
      sb.append(address.getCountryName()); 
     } 
     addressString = sb.toString(); 
     } catch (IOException e) {} 
    } else { 
     latLongString = "No location found"; 
    } 
    myLocationText.setText("Your Current Position is:\n" + 
          latLongString + "\n" + addressString); 
    } 
} 

인 위치 오버레이

import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.graphics.RectF; 
import android.location.Location; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.Projection; 

public class MyPositionOverlay extends Overlay { 

    private final int mRadius = 5; 

    Location location; 

    public Location getLocation() { 
    return location; 
    } 
    public void setLocation(Location location) { 
    this.location = location; 
    } 

    @Override 
    public boolean onTap(GeoPoint point, MapView mapView) { 
    return false; 
    } 

    @Override 
    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
    Projection projection = mapView.getProjection(); 

    if (shadow == false) { 
     // Get the current location  
     Double latitude = location.getLatitude()*1E6; 
     Double longitude = location.getLongitude()*1E6; 
     GeoPoint geoPoint; 
     geoPoint = new 
     GeoPoint(latitude.intValue(),longitude.intValue()); 

     // Convert the location to screen pixels  
     Point point = new Point(); 
     projection.toPixels(geoPoint, point); 

     RectF oval = new RectF(point.x - mRadius, point.y - mRadius, 
          point.x + mRadius, point.y + mRadius); 

     // Setup the paint 
     Paint paint = new Paint(); 
     paint.setARGB(250, 255, 255, 255); 
     paint.setAntiAlias(true); 
     paint.setFakeBoldText(true); 

     Paint backPaint = new Paint(); 
     backPaint.setARGB(175, 50, 50, 50); 
     backPaint.setAntiAlias(true); 

     RectF backRect = new RectF(point.x + 2 + mRadius, 
           point.y - 3*mRadius, 
           point.x + 65, point.y + mRadius); 

     // Draw the marker  
     canvas.drawOval(oval, paint); 
     canvas.drawRoundRect(backRect, 5, 5, backPaint); 
     canvas.drawText("Here I Am", 
         point.x + 2*mRadius, point.y, 
         paint); 
    } 
    super.draw(canvas, mapView, shadow); 
    } 
} 

이다

09-07 13:33:43.180: ERROR/AndroidRuntime(1811): FATAL EXCEPTION: main 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811): java.lang.NullPointerException 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at com.paad.whereami.MyPositionOverlay.draw(MyPositionOverlay.java:37) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at com.google.android.maps.Overlay.draw(Overlay.java:179) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:45) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at com.google.android.maps.MapView.onDraw(MapView.java:530) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.View.draw(View.java:9279) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewGroup.drawChild(ViewGroup.java:2584) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewGroup.drawChild(ViewGroup.java:2582) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewGroup.drawChild(ViewGroup.java:2582) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewGroup.drawChild(ViewGroup.java:2582) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.View.draw(View.java:9282) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.widget.FrameLayout.draw(FrameLayout.java:419) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1923) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewRoot.draw(ViewRoot.java:1695) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1410) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.view.ViewRoot.handleMessage(ViewRoot.java:2040) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.os.Looper.loop(Looper.java:132) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at android.app.ActivityThread.main(ActivityThread.java:4123) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at java.lang.reflect.Method.invoke(Method.java:491) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
09-07 13:33:43.180: ERROR/AndroidRuntime(1811):  at dalvik.system.NativeStart.main(Native Method) 
,536,
+0

포스트 로그 캣 출력과

if (shadow == false) { 

을하시기 바랍니다 대체합니다. – Reno

+0

로그 cat 출력을 추가했습니다. –

답변

2

null 포인터를 발생시키는 OS 버전이 아닌지 의심 스럽습니다. 오버레이 클래스의 '위치'가 그리기 호출시 null이 될 가능성이 큽니다. 추첨 방법에

if ((shadow == false) && (location != null)) { 
관련 문제