Android 安卓谷歌地图位置。添加一个新的锁定标记,并在谷歌地图上画一条线
嘿,伙计们,我一直在开发这个应用程序,wich有一个谷歌地图和我的位置,我所有这些都在工作 现在我想知道,我知道这是可能的,只是不知道如何。我如何在地图上的某个地方做一个新的标记(这个位置被锁定,不会改变发生的事情),最后当手机自动在你的位置和标记位置之间画一条线时 这是我的班级代码:Android 安卓谷歌地图位置。添加一个新的锁定标记,并在谷歌地图上画一条线,android,google-maps,gps,location,Android,Google Maps,Gps,Location,嘿,伙计们,我一直在开发这个应用程序,wich有一个谷歌地图和我的位置,我所有这些都在工作 现在我想知道,我知道这是可能的,只是不知道如何。我如何在地图上的某个地方做一个新的标记(这个位置被锁定,不会改变发生的事情),最后当手机自动在你的位置和标记位置之间画一条线时 这是我的班级代码: class MainActivity extends FragmentActivity implements LocationListener { GoogleMap googleMap; @Override
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(BitmapDescriptorFactory.fromResource(R.drawable.marker_wikipedia));
googleMap.addMarker(markerOptions);
正如您在我的应用程序中看到的:
在代码中,我认为您添加了两次相同的标记
然后,要在两点之间绘制一条线,可以按照中的示例进行操作:
我明白你的意思,这有点像我在想的,但是在你提到“每个地方”的标记选项中,你有一个类似于文本视图的东西,人们可以在一个随机的位置,你是否导入那个位置?比如,我可以把我的板条和经度坐标放在()里,还是不可能?还有一个问题,你把折线放在oncreate方法上面了吗?哦,对不起@user2422764,我已经更改了代码,将其与我的应用程序域分离。每个地方只是一个地方实体,有一个字符串和两个双倍的纬度和经度。我检索用户附近的许多位置,并将所有这些位置添加到地图中。我从来没有使用过多段线,但是Android文档中的例子很清楚,我想是的,这就是为什么我给你这个例子。我会在添加两个标记后添加多段线,但无论您以何种顺序将这些项目添加到地图中都无关紧要,因为您可以删除标记,因为这些标记都是独立的组件,所以线将在那里。哈哈,我看到您更改了代码,它现在可以工作了!非常感谢你!还有一个问题,你能看到我如何在“add(new LatLng(51.5,-0.1)”中得到我的cucerent location变量吗?:)我不明白。。你想让我在你的代码中看到什么?或者你想做什么却不能让它工作?
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));