【问题标题】:Android google map cuncerent location. Add a new locked marker and draw a line on the google mapAndroid 谷歌地图当前位置。添加一个新的锁定标记并在谷歌地图上画一条线
【发布时间】:2013-05-28 11:57:19
【问题描述】:

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

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

这是我的课程代码:

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

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

【问题讨论】:

    标签: android google-maps gps location


    【解决方案1】:

    要添加我正在做的标记:

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

    【讨论】:

    • 我明白你的意思,这有点像我在想的,但是在你的 Markeroptions 中提到“每个地方”,你有一个类似文本视图的东西,人们可以在其中但在一个随机的位置,然后你导入它位置与否?就像我可以将我的纬度和经度坐标放在()中还是不是这样?你把折线放在 oncreate 方法上面的其他问题?
    • 抱歉@user2422764,我已更改代码以将其与我的应用程序域分离。 eachPlace 只是一个带有字符串的 Place 实体和两个带有纬度和经度的双精度数。我检索了用户附近的一些位置并将所有这些位置添加到地图中。我从未使用过折线,但我认为 Android 文档中的示例很清楚,这就是我给你这个示例的原因。我会在添加两个标记后添加折线,但将这些项目添加到地图中的顺序并不重要,因为您可以删除标记并且线会在那里,因为它们是独立的组件
    • 啊哈哈我看到你改变了它现在可以工作的代码!非常感谢!
    • 再问一个问题,你能看到我如何在'add(new LatLng(51.5, -0.1)' 中获取我的位置变量吗?:)
    • 我不明白.. 你想让我在你的代码中看到什么?或者你想做什么而你不能让它工作?
    猜你喜欢
    • 1970-01-01
    • 2013-09-03
    • 2016-03-07
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    相关资源
    最近更新 更多