Android谷歌地图的位置。添加新的锁定标记并在Google地图上绘制一条线

时间:2013-05-28 11:57:19

标签: android google-maps gps location

嘿伙计们,所以我一直在研究这个应用程序,有一个谷歌地图,并得到我的位置,我有这一切工作,

现在我想知道,我知道这可能不是怎么回事。我怎样才能在地图上的某个地方创建一个新标记(这个位置被锁定,不会改变发生的事情),最后当手机有你的位置并且标记位置自动在它们之间画一条线?

这是我的班级代码:

class MainActivity extends FragmentActivity implements LocationListener {

GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    // Showing status
    if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    }else { // Google Play Services are available

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);

        // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(provider, 20000, 0, this);


    }


}



@Override
public void onLocationChanged(Location location) {

    TextView tvLocation = (TextView) findViewById(R.id.tv_location);

    // Getting latitude of the current location
    double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    // Showing the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    // Setting latitude and longitude in the TextView tv_location
    tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );



}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

这是我想加入

的想法
    Marker marker = googleMap.add(new MarkerOptions()
    .position(new LatLng(37.7750, 122.4183))
    .title("San Francisco")
    .snippet("Population: 776733"));

    googleMap.addMarker(marker);

我希望你们能帮助我,谢谢!

1 个答案:

答案 0 :(得分:2)

添加我正在做的标记:

MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(51.5, -0.1)).title("marker title").icon(BitmapDescriptorFact‌​ory.fromResource(R.drawable.marker_wikipedia));
googleMap.addMarker(markerOptions);

您可以在我的应用的line 348中看到:Tureame

在您的代码中,我认为您添加了两次相同的标记。

然后,要在两点之间画一条线,您可以按照Android documentation of Polyline中的示例:

GoogleMap map;
// ... get a map.
// Add a thin red line from London to New York.
Polyline line = map.addPolyline(new PolylineOptions()
    .add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
    .width(5)
    .color(Color.RED));