2016-09-23 2 views
-1

기본적으로 mapview를 사용하여 응용 프로그램에지도를 표시하고 있습니다. 대부분의지도 응용 프로그램과 같은 조각을 사용하지 마십시오. 하지만지도에 표식을 추가하는 데 문제가 있습니다. 도와주세요. MapView에 마커를 추가하는 방법

내는

private static final int GPS_ERROR_DIALOG_REQUEST = 9001; 
MapView mMapView; 
GoogleMap map; 
Context CTX = this; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if (servicesOK()) { 
     setContentView(R.layout.parents); 
     mMapView = (MapView) findViewById(R.id.map); 
     mMapView.onCreate(savedInstanceState); 
     Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show(); 
     map.addMarker(new MarkerOptions() 
       .position(new LatLng(4.588946, 101.125293)) 
       .title("You are here")); 
    } 
    else 
    { 
     Toast.makeText(this, "Not ready to map!", Toast.LENGTH_SHORT).show(); 
    } 

    Button AddUser = (Button) findViewById(R.id.user_reg); 
    Button KidsDelete = (Button) findViewById(R.id.user_del); 

    AddUser.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(Parents.this, AddUser.class)); 
     } 
    }); 
    KidsDelete.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(Parents.this, KidsDelete.class)); 
     } 
    }); 
} 

public boolean servicesOK() { 

    int isAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(CTX); 

    if (isAvailable == ConnectionResult.SUCCESS) { 
     return true; 
    } else if (GoogleApiAvailability.getInstance().isUserResolvableError(isAvailable)) { 
     Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(this, isAvailable, GPS_ERROR_DIALOG_REQUEST); 
     dialog.show(); 
    } else { 
     Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show(); 
    } 
    return false; 

} 

//For mapView 
@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    mMapView.onDestroy(); 
} 

@Override 
public void onLowMemory() { 
    super.onLowMemory(); 
    mMapView.onLowMemory(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    mMapView.onPause(); 
} 


@Override 
protected void onResume() { 
    super.onResume(); 
    mMapView.onResume(); 
} 

@Override 
protected void onSaveInstanceState(Bundle outstate) { 
    super.onSaveInstanceState(outstate); 
    mMapView.onSaveInstanceState(outstate); 
} 

코딩하고이 활동/조각에서 OnMapReadyCallback을 구현 오류

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.example.moon.mykidstracker, PID: 3031 
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moon.mykidstracker/com.example.moon.mykidstracker.Parents}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
         at android.os.Handler.dispatchMessage(Handler.java:102) 
         at android.os.Looper.loop(Looper.java:148) 
         at android.app.ActivityThread.main(ActivityThread.java:5417) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference 
         at com.example.moon.mykidstracker.Parents.onCreate(Parents.java:35) 
         at android.app.Activity.performCreate(Activity.java:6237) 
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
         at android.app.ActivityThread.-wrap11(ActivityThread.java)  
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
         at android.os.Handler.dispatchMessage(Handler.java:102)  
         at android.os.Looper.loop(Looper.java:148)  
         at android.app.ActivityThread.main(ActivityThread.java:5417)  
         at java.lang.reflect.Method.invoke(Native Method)  
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+0

귀하의'GoogleMap으로지도 onMapReady 방법에 비해

,''null' – Piyush

+0

은 어떻게 문제를 해결하는 것입니다? @Piyush – Zaidi

+0

Var는지도 초기화입니까? . 방금 Google지도 맵으로 선언했습니다. 덕분에 –

답변

0

입니다.

@Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     if (mMap != null) { 
      marker = mMap.addMarker(new MarkerOptions() 
        .position(latLng).title(place_name) 
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)) 
        .draggable(false).visible(true)); 
     } 
    } 
+0

. 지도는 작동하지만 여전히 마커가 없습니다 – Zaidi

+0

수정 된 답변을 시도하십시오 –

+0

지도를 추가 한 후에 해결되었습니다. = yourMapView.getMapAsync (this) ;. 감사합니다 : D – Zaidi

0
----------------------- 
     map = yourMapView.getMap(); 
+0

나는 getmap()이 이미 사용 중지되었습니다 – Zaidi

+0

getmapasync()를 사용하고 마커가 거기에 있습니다! :디 – Zaidi

관련 문제