2

嘿伙计们,所以我一直在开发这个应用程序,它有一个谷歌地图并获取我的位置,这一切都在工作,

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

这是我的课程代码:

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);

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

4

1 回答 1

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);

正如您在我的应用程序的第 348 行中看到的那样:Tureame

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

然后,要在两点之间画一条线,您可以按照Polyline 的 Android 文档中的示例进行操作:

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));
于 2013-05-28T12:29:09.333 回答