Android 谷歌地图当前位置。添加新的锁定标记并在谷歌地图上画一条线

标签 android google-maps gps location

嘿伙计们,我一直在开发这个应用程序,它有一个谷歌地图并获取我的位置,我已经完成了这一切,

现在我想知道,我知道这是可能的,只是不知道如何。我如何在 map 上的某个位置创建一个新标记(该位置已锁定,不会改变发生的情况),最后当手机有您的位置和标记位置时自动在它们之间画一条线?

这是我的类(class)代码:

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

希望大家能帮帮我,谢谢!

最佳答案

要添加我正在做的标记:

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

关于Android 谷歌地图当前位置。添加新的锁定标记并在谷歌地图上画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16791459/

相关文章:

java - 在访问游标中的数据之前,请确保游标已正确初始化。 sql 安卓

Android: TextView 超链接

java - 在 Emulator 上工作,但不在真正的 Android 设备上工作

android - 如何更快地开始接收 GPS?

java - java中的字符串长度

javascript - 谷歌地图仅自动完成带有编号的街道

Javascript 函数变量

javascript - 如何解决使用自动加载功能时多次包含谷歌地图的问题

android - NMEAListener 不工作?

android - 具有回调函数的网络请求,如何处理排队请求的结果