2016-09-23 35 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); 
} 

,这是错误

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的地图;''是null' – Piyush

+0

如何解决呢? @Piyush – Zaidi

+0

Var是初始化map吗? 。你刚刚声明为GoogleMap地图; –

回答

0

实现您的活动/片段OnMapReadyCallback

比onMapReady方法

@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

它解决了我添加map = yourMapView.getMapAsync(this);.感谢:D – Zaidi

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

我认为getmap()已经不推荐使用 – Zaidi

+0

我使用getmapasync()和标记在那里谢谢! :d – Zaidi