如何纪念经纬度在谷歌地图? [英] How to mark the Latitude,Longitude in Google Map ?

查看:221
本文介绍了如何纪念经纬度在谷歌地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌地图。我有一个数组,它有多个纬度和经度。我有纬度经度的中心点。所以我想,以纪念所有的经纬度自带10米距离中心点的半径内。

活动code:

  LocationManager LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
            位置位置= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            双经度= location.getLongitude();
            双纬度= location.getLatitude();



            的for(int i = 0;我小于10;我++){
                //随机纬度和logitude
                双[] randomLocation = createRandLocation(纬度,
                        经度);

                //添加标记
                MarkerOptions标记=新MarkerOptions()。位置(
                        新经纬度(randomLocation [0],randomLocation [1]))
                        .title伪(你好地图+ I);

                Log.e(随机,>中+ randomLocation [0] +,
                        + randomLocation [1]);



                如果(我== 0)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_AZURE));
                如果(ⅰ== 1)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_BLUE));
                如果(ⅰ== 2)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_CYAN));
                如果(我== 3)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_GREEN));
                如果(我== 4)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_MAGENTA));
                如果(我== 5)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_ORANGE));
                如果(我== 6)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_RED));
                如果(我== 7)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_ROSE));
                如果(我== 8)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_VIOLET));
                如果(我== 9)
                    marker.icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_YELLOW));
                googleMap.addMarker(标记);

                //将摄像机移动到最后的位置与缩放级别
                如果(我== 9){
                    CameraPosition cameraPosition =新CameraPosition.Builder()
                            的.target(新经纬度(randomLocation [0],
                                    randomLocation [1]))放大(15).build()。
                    googleMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
                }
            }

        }赶上(例外五){
            e.printStackTrace();
        }
    }

    @覆盖
    保护无效onResume(){
        super.onResume();
        initilizeMap();
    }


    私人无效initilizeMap(){
        如果(GoogleMap的== NULL){
            GoogleMap的=((MapFragment)getFragmentManager()。findFragmentById(
                    R.id.map))的GetMap()。

            //如果映射成功与否创建检查
            如果(GoogleMap的== NULL){
                Toast.makeText(getApplicationContext(),
                        对不起!无法创建地图,Toast.LENGTH_SHORT)
                        。显示();
            }
        }
    }


    私人双[] createRandLocation(双纬度,经度两倍){
        返回新的双[] {纬度+((的Math.random() -  0.5)/ 500),
                经度+((的Math.random() -  0.5)/ 500),
                150 +((的Math.random() -  0.5)* 10)};

    }
}
 

解决方案

首先创建您的中心点,如:

 圈圈= map.addCircle(新CircleOptions()
 .center(新经纬度(centerpointLat,cnterpointLong))
 .radius(10)
 .strokeColor(Color.RED)
 );
 

现在,找到距离点的中心并存储到一个阵列的ArrayList 这样的:

 浮法[]距离=新的浮动[1];
最后的ArrayList<整数GT; ArrayList的=新的ArrayList<整数GT;();
的for(int i = 0;我小于10;我++){

双[] randomLocation = createRandLocation(纬度,
                    经度);

Location.distanceBetween(randomLocation [0],randomLocation [1],
            。circle.getCenter()纬度,circle.getCenter()经度,距离)。
 arrayList.add((int)的结果[0]);
 }
 

现在,检查距离的中心为< 10这样的:

 的for(int i = 0;我小于10;我++){

  如果(arrayList.get(ⅰ)所述; circle.getRadius()或(直接提供(圆半径))){
         //添加标记属于这里
        } 其他 {
         //任何信息在这里
        }
  }
 

试试这个方法

更新:这是你的活动

 公共类BasicMapActivity_new2扩展FragmentActivity工具
    LocationListener的,OnMarkerClickListener {

私人GoogleMap的MMAP;
私人LocationManager locationManager;
私人字符串提供商;
双纬度,经度;
圈圈;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.basic_demo);
    setHeaderBackground();

    MMAP =((SupportMapFragment)getSupportFragmentManager()
            .findFragmentById(R.id.map2))的GetMap()。
    mMap.setMyLocationEnabled(真正的);

    LocationManager服务=(LocationManager)getSystemService(LOCATION_SERVICE);
    布尔enabledGPS =服务
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    布尔enabledWiFi =服务
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    如果(!enabledGPS){
        Toast.makeText(BasicMapActivity_new2.this,GPS信号未找到,
                Toast.LENGTH_LONG).show();
        意向意图=新的意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(意向);
    }否则,如果(!enabledWiFi){
        Toast.makeText(BasicMapActivity_new2.this,
                网络信号没有发现,Toast.LENGTH_LONG).show();
        意向意图=新的意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(意向);
    }

    locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    //定义标准如何选择locatioin提供商 - >使用
    // 默认
    标准标准=新标准();
    供应商= locationManager.getBestProvider(标准,FALSE);
    位置位置= locationManager.getLastKnownLocation(供应商);
    // getCurrentLocation();

    //初始化位置字段
    如果(位置!= NULL){
        // Toast.makeText(BasicMapActivity_new.this,选商+
        //提供商,
        // Toast.LENGTH_SHORT).show();
        onLocationChanged(位置);
    } 其他 {

        // 做一点事
    }
    // setUpMapIfNeeded();
    setUpMap();
}

@覆盖
保护无效的onPause(){
    super.onPause();
    locationManager.removeUpdates(本);
}

@覆盖
保护无效onResume(){
    super.onResume();
    locationManager.requestLocationUpdates(供应商,400,1,本);
    setUpMapIfNeeded();
}


私人无效setUpMap(){
    mMap.getUiSettings()setCompassEnabled(真)。
    mMap.getUiSettings()setTiltGesturesEnabled(真)。
    mMap.getUiSettings()setRotateGesturesEnabled(真)。
    mMap.getUiSettings()setScrollGesturesEnabled(真)。
    mMap.getUiSettings()setZoomControlsEnabled(真)。
    mMap.getUiSettings()setZoomGesturesEnabled(真)。
    SMSLocation();
}

公共无效SMSLocation(){

    圈圈= mMap.addCircle(新CircleOptions()
            .center(新经纬度(纬度,经度))。半径(100)
            .strokeColor(Color.RED));

    浮动[]距离=新的浮动[1];
    最后的ArrayList<整数GT; ArrayList的=新的ArrayList<整数GT;();

    的for(int i = 0;我小于10;我++){
        //随机纬度和logitude
        双[] randomLocation = createRandLocation(纬度,经度);

        //添加标记
        MarkerOptions标记=新MarkerOptions()。位置(
                新经纬度(randomLocation [0],randomLocation [1]))。标题(
                你好地图+ I);

        Location.distanceBetween(randomLocation [0],randomLocation [1],
                circle.getCenter(),纬度,circle.getCenter()。经度,
                距离);

        // arrayList.add((int)的距离[0]);
        的System.out.println(纬度:+ randomLocation [0] +,
                + randomLocation [1]);

        Log.e(随机,>中+ randomLocation [0] +,+ randomLocation [1]);
        Toast.makeText(
                BasicMapActivity_new2.this,
                随机位置+ randomLocation [0] +,
                        + randomLocation [0],Toast.LENGTH_LONG).show();

        如果(我== 0)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_AZURE));
        如果(ⅰ== 1)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_BLUE));
        如果(ⅰ== 2)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_CYAN));
        如果(我== 3)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_GREEN));
        如果(我== 4)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_MAGENTA));
        如果(我== 5)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_ORANGE));
        如果(我== 6)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_RED));
        如果(我== 7)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_ROSE));
        如果(我== 8)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_VIOLET));
        如果(我== 9)
            marker.icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_YELLOW));

        如果(距离[0]&其中; circle.getRadius()){
            //添加标记属于这里
            mMap.addMarker(标记);
        }
        //将摄像机移动到最后的位置与缩放级别
        / *
         *如果(我== 9){CameraPosition cameraPosition =新
         * CameraPosition.Builder()的.target(新经纬度(randomLocation [0],
         * randomLocation [1]))放大(15).build();
         * mMap.animateCamera(CameraUpdateFactory
         * .newCameraPosition(cameraPosition)); }
         * /
    }

}

@覆盖
公共布尔onMarkerClick(标记为arg0){
    // TODO自动生成方法存根
    返回false;
}

标记startPerc = NULL;
地点old_one;

@覆盖
公共无效onLocationChanged(位置定位){

    // mMap.clear();

    双纬度= location.getLatitude();
    双LNG = location.getLongitude();
    // Toast.makeText(BasicMapActivity_new.this,位置+纬度+,+ LNG,
    // Toast.LENGTH_LONG).show();
    经纬度坐标=新的经纬度(纬度,经度);
    纬度=土地增值税;
    经度= LNG;

    Toast.makeText(BasicMapActivity_new2.this,
            位置+ coordinate.latitude +,+ coordinate.longitude,
            Toast.LENGTH_LONG).show();

    startPerc = mMap.addMarker(新MarkerOptions()
            .position(坐标)
            .title伪(当前位置)
            .icon(BitmapDesc​​riptorFactory
                    .defaultMarker(BitmapDesc​​riptorFactory.HUE_BLUE)));

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(坐标,18.0f));

}

私人双[] createRandLocation(双纬度,经度两倍){
    返回新的双[] {纬度+((的Math.random() -  0.5)/ 500),
            经度+((的Math.random() -  0.5)/ 500),
            150 +((的Math.random() -  0.5)* 10)};

}

@覆盖
公共无效onProviderDisabled(字符串提供商){
    // TODO自动生成方法存根

}

@覆盖
公共无效onProviderEnabled(字符串提供商){
    // TODO自动生成方法存根

}

@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
    // TODO自动生成方法存根

}

}
 

下面是 layout.xml 文件

 < XML版本=1.0编码=UTF-8&GT?;

< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<片段
    机器人:ID =@ + ID / MAP2
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    类=com.google.android.gms.maps.SupportMapFragment/>

< / LinearLayout中>
 

不要忘了添加许可的manifest.xml

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
 

输出:

I am using Google Map. I have a array that have multiple Latitude and Longitude. I have a center point of Latitude Longitude. So I want to mark all the Latitude ,Longitude that comes inside the radius of 10 meters from the center point.

Activity Code:

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

    }
}

解决方案

First of all create Circle with your Center Point like:

Circle circle = map.addCircle(new CircleOptions()
 .center(new LatLng(centerpointLat, cnterpointLong))
 .radius(10)
 .strokeColor(Color.RED)
 );

Now, find Distance from Center of the Circle to your Points and store into one Array or ArrayList like :

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

Now, check the Distance from the Center of the Circle is <10 like:

  for(int i=0;i<10;i++){

  if(arrayList.get(i) < circle.getRadius() or (directly provide(circle radius))){
         //Add Marker belong here   
        } else {
         //Any Message over here  
        }
  } 

Try this way

Update: Here is your Activity

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

}

}

Here is layout.xml file

<?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>

And do not forget to add permission into manifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Output:

这篇关于如何纪念经纬度在谷歌地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆