如何显示/绘制使用的MarkerOptions在谷歌Android地图API v2中的位置 [英] How to display/Plot the Locations using MarkerOptions in android Google Maps API v2

查看:1333
本文介绍了如何显示/绘制使用的MarkerOptions在谷歌Android地图API v2中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何显示/绘制在地图中的位置上有问题。我想提出一个标记在每一个特定的位置。我创建SQLite数据库来存储我的数据(MallName,地址,经度和纬度)。一切工作正常。它从我的sqlite的返回值。我测试了它使用日志。见MallMarkers.java

I have a problem on how to display/plot the locations in map. I want to put a marker in every specific locations. I created sqlite database to store my data(MallName, address, latitude and longitude). Everything works fine. It returns values from my sqlite. I tested it using Log. See MallMarkers.java

也许我用的MarkerOptions犯了一个错误。请帮帮我。
这里是我的code。

Maybe I made a mistake in using MarkerOptions. Please help me. Here's my Code.

类GoogleMapsApiActivity.java

Class GoogleMapsApiActivity.java

public class GoogleMapsApiActivity extends FragmentActivity implements LocationListener{

private ArrayList<MallMarkers> malls = new ArrayList<MallMarkers>();
private LocationManager locationManager;
private GoogleMap googleMap;
private static final String DEBUG_TAG = "GoogleMapsApiActivity";
private Location myLocation = null;
private UiSettings mapInterface;
private boolean zoom = true;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000*60*1;
private DataSource datasource;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_api);
    zoom = true;
    GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
}   
private void setUpMapIfNeeded() {
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        if (googleMap != null) {
            setUpMapSetings();              
        }
    }
}

private void setUpMapSetings(){         
    SharedPreferences sharedMapSettings = PreferenceManager.getDefaultSharedPreferences(this);
    String  mapViewLayout = sharedMapSettings.getString("map_layer", "Normal");
    Boolean mapTraffic = sharedMapSettings.getBoolean("key_mapTraffic", false);
    Boolean mapCompass = sharedMapSettings.getBoolean("key_mapCompass", true);
    boolean mapMyLocation = sharedMapSettings.getBoolean("key_mapMyLocation", true);
    boolean mapButtonZoom = sharedMapSettings.getBoolean("key_mapButtonZoom", true);
    mapInterface = googleMap.getUiSettings();
    //plotMalls();
    if(mapViewLayout.equals("Normal")){
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }
    else if(mapViewLayout.equals("Hybrid")){
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }
    else if(mapViewLayout.equals("Satellite")){
        googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }
    else if(mapViewLayout.equals("Terrain")){
        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    }

    googleMap.setTrafficEnabled(mapTraffic);
    mapInterface.setCompassEnabled(mapCompass);
    mapInterface.setMyLocationButtonEnabled(mapMyLocation);
    mapInterface.setZoomControlsEnabled(mapButtonZoom);

    googleMap.getUiSettings().setAllGesturesEnabled(true);
    googleMap.setMyLocationEnabled(true);
}  
//I used cursor to query my data from sqlite (Works fine)
private void plotLocationMallMapsUI(){
    database=openOrCreateDatabase(com.example.sqlite.MySQLiteHelper.DATABASE_NAME, Context.MODE_PRIVATE, null);
    String sqlcommand = "SELECT MallName, MallStreetAddress, MallCityAddress, MallContactNumber, MallLatitude, MallLongitude FROM mallsinfo";
    Cursor cursor = database.rawQuery(sqlcommand, null);
    malls = new ArrayList<MallMarkers>();
    if(cursor.moveToFirst()){       
        do{
            MallMarkers markers = new MallMarkers(cursor.getString(0),
            cursor.getString(1),
            cursor.getString(2),
            cursor.getString(3),
            cursor.getString(4),
            cursor.getString(5), this);
            malls.add(markers);
        }while(cursor.moveToNext());        
    }
    cursor.close();
    database.close();
}

@Override
public void onRestart() {
    super.onRestart(); 
    setUpMapSetings();
}
@Override 
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
    plotLocationMallMapsUI();

}
@Override
public void onPause() {
    locationManager.removeUpdates(this);
    super.onPause();
}
@Override
public void onLocationChanged(Location location) {
    LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    location = locationManager.getLastKnownLocation(provider);
    double lat = (double) (location.getLatitude());
    double lng = (double) (location.getLongitude());
    Log.d(DEBUG_TAG, "Latitude: " + String.valueOf(lat));
    Log.d(DEBUG_TAG, "Longitude: " + String.valueOf(lng));
}
@Override
public void onProviderDisabled(String provider) {

}
@Override
public void onProviderEnabled(String provider) {

} 
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}       

}

据从SQLite的返回值。我测试了它使用日志。但它不显示的情节。请帮帮我。如何使用标记来显示呢?

It returns values from sqlite. I tested it using Log. But it doesnt display the Plots. Help me please. How to display it using markers?

类MallMarkers.java

Class MallMarkers.java

public class MallMarkers {
private String mMallId = "";
private String mMallName = "";
private String mMallStreetAddress = "";
private String mMallCityAddress = "";
private String mMallContactNumber = "";
private String mMallLatitude = "";
private String mMallLongitude = "";
private Context mContext;
private LatLng mMallCoordinates = null;
private MarkerOptions mMallMarkerOptions=null;
private Double mLatitude;
private Double mLongitude;
private GoogleMap googlemap;
public MallMarkers(String mallname, String mallstreetaddress, String mallcityaddress, String mallcontactnumber, String malllatitude, String malllongitude, Context context){
    mContext = context;
    mMallName = mallname;
    mMallStreetAddress = mallstreetaddress;
    mMallCityAddress = mallcityaddress;
    mMallContactNumber = mallcontactnumber;
    mMallLatitude = malllatitude;
    mMallLongitude = malllongitude;
    mLatitude = Double.parseDouble(mMallLatitude);
    mLongitude = Double.parseDouble(mMallLongitude);

    String FilterMallName;
    String FilterMallStreetAddress;
    String FilterMallCityAddress;
    String FilterMallContacts;
        if(mMallName.equals("")){
            FilterMallName = "Blank";
        }else{
            FilterMallName = mMallName;
        }

        if(mMallStreetAddress.equals("")){
            FilterMallStreetAddress = "Blank";
        }else{
            FilterMallStreetAddress = mMallStreetAddress;
        }

        if(mMallCityAddress.equals("")){
            FilterMallCityAddress = "Blank";
        }else{
            FilterMallCityAddress = mMallCityAddress;
        }

        if(mMallContactNumber.equals("")){
            FilterMallContacts = "Blank";
        }else{
            FilterMallContacts = mMallContactNumber;
        }


        Log.d("TAG", FilterMallName + ", \n"
                + FilterMallStreetAddress + ", \n" 
                + FilterMallCityAddress+ ", \n" 
                + FilterMallContacts + "\n" 
                + String.valueOf(mLatitude) + " ," + String.valueOf(mLongitude) 
                + "\n------------------------\n");

        mMallCoordinates = new LatLng(mLatitude, mLongitude);
        mMallMarkerOptions = new MarkerOptions();
        mMallMarkerOptions.position(mMallCoordinates);
        mMallMarkerOptions.title(FilterMallName);
        mMallMarkerOptions.snippet(FilterMallStreetAddress + ", " + FilterMallCityAddress + ", " + FilterMallContacts);
        mMallMarkerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));

}

}

推荐答案

下面是一个循环,我设置标记在地图上,它为我的伟大工程:

here is a loop where I set markers on the map and it works great for me:

for (Task tempTask : TasksListAppObj.getInstance().tasksRepository.getTasksRepository())
                {
                    LatLng latlng = new LatLng(tempTask.getLatitude(), tempTask.getLongtitude());
                    if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_WAITING))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_blue)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_IN_PROGRESS))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_bordo)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_ON_THE_WAY))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_turkiz)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_COMPLETE))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_orange)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_FAILED))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));
                    }
}

看看它是否可以帮助你。

See if it's helps you.

的if语句改变标记图标。

The if statements are for changing the marker icon.

你所缺少的是应用到地图实际添加标记对象 addMarker 方法的GoogleMap 对象。

What you are missing is the addMarker method applied to the map to actually add the Marker object to GoogleMap object.

这篇关于如何显示/绘制使用的MarkerOptions在谷歌Android地图API v2中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆