大家好我想在我的谷歌地图屏幕上显示多个标记。我将lat-long测试名称值存储在数据库中。我可以从数据库中获得结果,但是在谷歌地图上引脚不可用。
这是返回游标的数据库方法....
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
答案 0 :(得分:1)
在地图版本2中,谷歌地图显示多个标记,您只需添加
mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(lat, longitude)).title("Pin title"));
for for循环中的不同纬度/经度值。其中mGoogleMap是V2中GoogleMap的对象
请检查您的行为是否相应。
答案 1 :(得分:0)
我在错误的地方调用setPinOnMap()方法。因为之前没有创建地图对象所以它无法显示标记。
一旦我在地图对象创建后调用它就像魅力一样。
我在代码中进行了以下更改,它正在运行......
if(mPlayServiceStatus)
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)).getMap();
Log.d("MAP","Coming inside on onCreate()");
/**Add pin to map */
setPinOnMap();
答案 2 :(得分:0)
试试这个:
{{1}}