Android 如何在谷歌地图中标记纬度、经度?
我正在使用谷歌地图。我有一个具有多个纬度和经度的数组。我有一个经纬度的中心点。所以我想标记所有的纬度,经度,在距离中心点10米的半径范围内 活动代码:Android 如何在谷歌地图中标记纬度、经度?,android,google-maps,google-maps-markers,google-maps-android-api-2,Android,Google Maps,Google Maps Markers,Google Maps Android Api 2,我正在使用谷歌地图。我有一个具有多个纬度和经度的数组。我有一个经纬度的中心点。所以我想标记所有的纬度,经度,在距离中心点10米的半径范围内 活动代码: LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
for (int i = 0; i < 10; i++) {
// random latitude and logitude
double[] randomLocation = createRandLocation(latitude,
longitude);
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(randomLocation[0], randomLocation[1]))
.title("Hello Maps " + i);
Log.e("Random", "> " + randomLocation[0] + ", "
+ randomLocation[1]);
if (i == 0)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
if (i == 1)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
if (i == 2)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
if (i == 3)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
if (i == 4)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
if (i == 5)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
if (i == 6)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
if (i == 7)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
if (i == 8)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
if (i == 9)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
googleMap.addMarker(marker);
// Move the camera to last position with a zoom level
if (i == 9) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(randomLocation[0],
randomLocation[1])).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
}
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
Location Location=lm.getLastKnownLocation(LocationManager.GPS\U提供程序);
double longitude=location.getLongitude();
双纬度=location.getLatitude();
对于(int i=0;i<10;i++){
//随机纬度和逻辑度
double[]randomLocation=createRandLocation(纬度,
经度);
//添加标记
MarkerOptions marker=新MarkerOptions().位置(
新LatLng(随机位置[0],随机位置[1]))
.标题(“你好地图”+i);
Log.e(“随机”、“>”+随机位置[0]+“,”
+随机定位[1]);
如果(i==0)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
如果(i==1)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
如果(i==2)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_-CYAN));
如果(i==3)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
如果(i==4)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_-Pigenta));
如果(i==5)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
如果(i==6)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
如果(i==7)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_-ROSE));
如果(i==8)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_-VIOLET));
如果(i==9)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_-YELLOW));
谷歌地图。添加标记(标记);
//使用缩放级别将相机移动到最后一个位置
如果(i==9){
CameraPosition CameraPosition=新建CameraPosition.Builder()
.目标(新车床)(随机位置[0],
randomLocation[1])).zoom(15.build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
}
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
受保护的void onResume(){
super.onResume();
initilizeMap();
}
私有void initilizeMap(){
if(googleMap==null){
googleMap=((MapFragment)getFragmentManager().findFragmentById(
R.id.map)).getMap();
//检查是否成功创建映射
if(googleMap==null){
Toast.makeText(getApplicationContext(),
“抱歉!无法创建地图”,Toast.LENGTH\u SHORT)
.show();
}
}
}
专用双[]createRandLocation(双纬度、双经度){
返回新的双精度[]{latitude+((Math.random()-0.5)/500),
经度+((Math.random()-0.5)/500),
150+((Math.random()-0.5)*10)};
}
}
首先,用你的中心点创建一个圆,如:
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(centerpointLat, cnterpointLong))
.radius(10)
.strokeColor(Color.RED)
);
现在,找到从圆的中心到点的距离
,并存储到一个数组
或数组列表
中,如下所示:
float[] distances = new float[1];
final ArrayList<Integer> arrayList = new ArrayList<Integer>();
for(int i=0;i<10;i++){
double[] randomLocation = createRandLocation(latitude,
longitude);
Location.distanceBetween(randomLocation[0],randomLocation[1],
circle.getCenter().latitude, circle.getCenter().longitude, distances);
arrayList.add((int) results[0]);
}
这样试试
更新:这是您的活动
public class BasicMapActivity_new2 extends FragmentActivity implements
LocationListener, OnMarkerClickListener {
private GoogleMap mMap;
private LocationManager locationManager;
private String provider;
Double Latitude, longitude;
Circle circle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setHeaderBackground();
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map2)).getMap();
mMap.setMyLocationEnabled(true);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enabledWiFi = service
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!enabledGPS) {
Toast.makeText(BasicMapActivity_new2.this, "GPS signal not found",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else if (!enabledWiFi) {
Toast.makeText(BasicMapActivity_new2.this,
"Network signal not found", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// getCurrentLocation();
// Initialize the location fields
if (location != null) {
// Toast.makeText(BasicMapActivity_new.this, "Selected Provider " +
// provider,
// Toast.LENGTH_SHORT).show();
onLocationChanged(location);
} else {
// do something
}
// setUpMapIfNeeded();
setUpMap();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
setUpMapIfNeeded();
}
private void setUpMap() {
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setTiltGesturesEnabled(true);
mMap.getUiSettings().setRotateGesturesEnabled(true);
mMap.getUiSettings().setScrollGesturesEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
SMSLocation();
}
public void SMSLocation() {
Circle circle = mMap.addCircle(new CircleOptions()
.center(new LatLng(Latitude, longitude)).radius(100)
.strokeColor(Color.RED));
float[] distances = new float[1];
final ArrayList<Integer> arrayList = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
// random latitude and logitude
double[] randomLocation = createRandLocation(Latitude, longitude);
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(randomLocation[0], randomLocation[1])).title(
"Hello Maps " + i);
Location.distanceBetween(randomLocation[0], randomLocation[1],
circle.getCenter().latitude, circle.getCenter().longitude,
distances);
// arrayList.add((int) distances[0]);
System.out.println("latitude: " + randomLocation[0] + ", "
+ randomLocation[1]);
Log.e("Random", "> " + randomLocation[0] + ", " + randomLocation[1]);
Toast.makeText(
BasicMapActivity_new2.this,
" Random Location " + randomLocation[0] + ","
+ randomLocation[0], Toast.LENGTH_LONG).show();
if (i == 0)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
if (i == 1)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
if (i == 2)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
if (i == 3)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
if (i == 4)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
if (i == 5)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
if (i == 6)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED));
if (i == 7)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
if (i == 8)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
if (i == 9)
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
if (distances[0] < circle.getRadius()) {
// Add Marker belong here
mMap.addMarker(marker);
}
// Move the camera to last position with a zoom level
/*
* if (i == 9) { CameraPosition cameraPosition = new
* CameraPosition.Builder() .target(new LatLng(randomLocation[0],
* randomLocation[1])).zoom(15).build();
* mMap.animateCamera(CameraUpdateFactory
* .newCameraPosition(cameraPosition)); }
*/
}
}
@Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
return false;
}
Marker startPerc = null;
Location old_one;
@Override
public void onLocationChanged(Location location) {
// mMap.clear();
double lat = location.getLatitude();
double lng = location.getLongitude();
// Toast.makeText(BasicMapActivity_new.this, "Location " + lat+","+lng,
// Toast.LENGTH_LONG).show();
LatLng coordinate = new LatLng(lat, lng);
Latitude = lat;
longitude = lng;
Toast.makeText(BasicMapActivity_new2.this,
"Location " + coordinate.latitude + "," + coordinate.longitude,
Toast.LENGTH_LONG).show();
startPerc = mMap.addMarker(new MarkerOptions()
.position(coordinate)
.title("Current Location")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 18.0f));
}
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
@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
}
}
并且不要忘记将权限添加到manifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
输出:您已经有了一个中心点。right@MD是的,我有一个中心。我只想匹配阵列中的lat和日志,并将它们标记在半径中。这是我的解决方案。首先用所有的lat和long点创建一个圆
。现在,找到点到圆
中心的距离,最后检查距离,如:if(distance[0]<10){Toast.makeText(getBaseContext(),“Inside”,Toast.LENGTH_long)。show();}else{Toast.makeText(getBaseContext(),“外部”,Toast.LENGTH_LONG.show();}
你能发布准确的代码吗?我没有得到这个。我必须把这个代码放在哪里。请帮我检查我的答案,然后用这种方式试试。@GauravPandey这里的问题不是关于我的代码,而是关于获取当前位置的手机传感器,因为它在我的Note II、Galaxy Tab2和Nexus 4中工作,但不幸的是,它在Micromax A90Si a中不工作m在每个设备上都面临相同的问题没有变化,因为圆圈是从当前位置创建的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />