1

こんにちは、Google マップ画面に複数のマーカーを表示したいと考えています。緯度と経度のテスト名の値をデータベースに保存しています。データベースから結果を取得できますが、Google マップでピンが表示されません。

これは、カーソルを返すデータベース メソッドです....

public Cursor getPinFromDatabase(String testType)
{
    Cursor results = null;
    try{
        mDatabase = mDbHelper.getReadableDatabase();
        String selection = GLNetTestDataContract.TestPinRecord.COLUMN_NAME_TESTTYPE + "=?";
        // Specify arguments in placeholder order.
        String[] selectionArgs = { String.valueOf(testType)};
        results = mDatabase.query(GLNetTestDataContract.TestPinRecord.TABLE_NAME, null, selection, selectionArgs, null, null, null);
        results.moveToFirst();
        do
        {
            mLogger.printLog("pin","TestName : "+results.getString(1));
            mLogger.printLog("pin","TestType : "+results.getString(2));
            mLogger.printLog("pin","Latitude : "+results.getString(3));
            mLogger.printLog("pin","Longitude: "+results.getString(4));
        }while(results.moveToNext());           
    }
    catch(Exception e)
    {
        mLogger.printLog("pin", "Exception in TestPinRecordHandler->>getPinFromDatabase");
        mLogger.printLog("pin", e.getLocalizedMessage());
    }
    finally{
        mDatabase.close();
        return results;
    }

}

}

このメソッドは、マップ上にピンを設定する役割を果たします setPinOnMap()

public void setPinOnMap(){
        try{
            Cursor pinRecordCursor = mTestPinRecordHandler.getPinFromDatabase("Manual");
            mPinMarkerList = new ArrayList<PinMarker>();
            if(pinRecordCursor != null && pinRecordCursor.moveToFirst())
            {
                do
                {//String testName, String testType, String latitude, String longitude
                    PinMarker markers = new PinMarker(pinRecordCursor.getString(1),pinRecordCursor.getString(2),
                            pinRecordCursor.getString(3),pinRecordCursor.getString(4));
                    mPinMarkerList.add(markers);
                }while(pinRecordCursor.moveToNext());
                pinRecordCursor.close();
                for  (GLNetTestPinMarker mm : mPinMarkerList) {
                    mMap.addMarker(mm.getMarkerOption());
                }
            }
        }
        catch(Exception e)
        {
            mLogger.printLog("pin", "Exception in MapHandler-->setPinOnMap");
            mLogger.printLog("pin", e.getMessage());
        }
    }

これがPinMarkerのコードです.....

public class PinMarker {

private LatLng mMallCoordinates = null;
private MarkerOptions mTestPinOptions=null;
private Double mLatitude;
private Double mLongitude;
private GoogleMap googlemap;
private static Logger mLogger = null;

public PinMarker(String testName, String testType, String latitude, String longitude)
{
    mLatitude = Double.parseDouble(latitude);
    mLongitude = Double.parseDouble(longitude);
    mMallCoordinates = new LatLng(mLatitude, mLongitude);
    mTestPinOptions = new MarkerOptions();
    mTestPinOptions.position(mMallCoordinates);
    mTestPinOptions.title(testName);
    mTestPinOptions.snippet( latitude + ", " + latitude);      
    if(testType.equals("Manual"))
        mTestPinOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red));
    if(mLogger==null)
        mLogger = new Logger();
    mLogger.printLog("pin", "-----------------------GLNetTestPinMarker-----------------------");
    mLogger.printLog("pin", testName+ " : "+ testType +" : "+ latitude+" : "+longitude);

}

public MarkerOptions getMarkerOption()
{
    return mTestPinOptions;

}

}

これは onCreate() メソッドで行っています。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("MAP","Coming inside on onCreate()");
    mTestPinRecordHandler = new TestPinRecordHandler(getApplicationContext());
    mLogger = new Logger();
    setContentView(R.layout.activity_map_sample);
    /**Add pin to map */
    setPinOnMap();

    /*********************************************************************************************************/
    try{
        mPlayServiceStatus = checkGooglePlayServicesAvailability();
    }
    catch(Exception e)
    {
        System.out.println("Kindly update the Google Play service");
    }
    String lPreviousZoomLevelString = Config.getSetting(getApplicationContext(), "MAPZOOMLEVEL");
    if(lPreviousZoomLevelString == null || lPreviousZoomLevelString.length() == 0)
    {
        Config.setSetting(getApplicationContext(), "MAPZOOMLEVEL", Float.toString(mPreviousZoomLevel));
    }
    if(mPlayServiceStatus)
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)).getMap();
//      mMap.setMyLocationEnabled(true);
    Log.d("MAP","Coming inside on onCreate()");
    /** Create GPSListener object to handle GPS values */
    mGPSListener = new GPSListener(getApplicationContext());
//      if(Integer.parseInt(Config.getSetting(getApplicationContext(), "KEEPALIVEDURATION"))<=5)
//      {
        try{
        GPSListener.UseLastKnownLocation();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
//      }
    /** Handle the setup button or tab **/
    Button setup = (Button) findViewById(R.id.setup);
    setup.setOnClickListener(new configclicker()); 

    /** Handle the manualtest button or tab **/
    Button mantest = (Button) findViewById(R.id.mantest);
    mantest.setOnClickListener(new manualtestclicker()); 

    /** Handle the home button or tab **/
    Button home = (Button) findViewById(R.id.home);
    home.setOnClickListener(new homeclicker());   

    /** Handle the map button or tab **/
    Button mapView = (Button) findViewById(R.id.maps );
    mapView.setEnabled(false);
    mapView.setPressed(false);        
    if(!isGoogleMapsInstalled())
    {            
        Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Please install Google Maps");
        builder.setCancelable(false);
        builder.setPositiveButton("Install", getGoogleMapsListener());
        AlertDialog dialog = builder.create();
        dialog.show();        
    }
    try{
        if(mPlayServiceStatus){
            mMap.setOnCameraChangeListener(new OnCameraChangeListener (){

                @Override
                public void onCameraChange(CameraPosition lcameraposition) {
                    // TODO Auto-generated method stub
                    if(mPreviousZoomLevel != lcameraposition.zoom && CommandHandler.mLoadingMapFirstTime )
                    {
                        CommandHandler.mLoadingMapFirstTime = false;
                    }
                    else
                    {
                        mPreviousZoomLevel = lcameraposition.zoom;
                    }
                    Config.setSetting(getApplicationContext(), "MAPZOOMLEVEL", Float.toString(mPreviousZoomLevel));
                }

            });
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("Exception 1: "+ e.getMessage());
    }
    /** Gray the current selected button or tab i.e about button **/
    Button about = (Button) findViewById(R.id.about);
    about.setOnClickListener(new abouttestclicker());
    /**Set current location in google map*/
    if(mPlayServiceStatus)
        setCurrentLocation();       

    mHandler = new Handler();
    if(mPlayServiceStatus){
        mHandler.removeCallbacks(updateCenterTask);
        mHandler.postDelayed(updateCenterTask, 100);
    }
}

だから私はどこで間違いを犯していますか。https://developers.google.com/maps/documentation/android/marker を参照しました

4

3 に答える 3

0

間違った場所で setPinOnMap() メソッドを呼び出していました。マップ オブジェクトが以前に作成されていなかったため、マーカーを表示できませんでした。

マップオブジェクトが作成された後に呼び出すと、魅力的に機能します。

コードに次の変更を加えましたが、機能していました...

if(mPlayServiceStatus)
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)).getMap();
Log.d("MAP","Coming inside on onCreate()");
    /**Add pin to map */
    setPinOnMap();
于 2013-07-30T05:51:09.047 に答える