婚恋系统源码,如何成功获取用户的定位

婚恋系统源码使用Google提供的LocationManager类。不过只能获取坐标,需要自己多坐标进行处理。
我同样是以服务的形式调用。

//添加定位权限

   <!-- 这个权限用于访问GPS定位-->
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

package com.greysun.he.service;

import com.greysun.he.bin.AppSystem;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.util.Log;

public class ListenSeat extends Service{
    private LocationManager manager;
    private LocationListener locationListener;

    @Override
    public int onStartCommand(Intent intent,int flags,int startId){
        System.out.println("开启位置监听"+AppSystem.getListenerNumber());

        manager = (LocationManager)getSystemService(LOCATION_SERVICE);
        Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        //第一次获得设备的位置
        updateLocation(location);

        locationListener = new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {
                updateLocation(location);
            }

            @Override
            public void onProviderDisabled(String str) {
                System.out.println(str);
            }

            @Override
            public void onProviderEnabled(String str) {
                System.out.println(str);
            }

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                System.out.println(arg0+" int: " +arg1+ " Bundle " +arg2);
            }   

        };

        //重要函数,监听数据测试
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, locationListener);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    private void updateLocation(Location location) {
        String latLng;

        if (location != null) {
            double lat = location.getLatitude();   
            double lng = location.getLongitude();

            latLng = "Latitude:" + lat + "  Longitude:" + lng;
        } else {   
            latLng = "Can't access your location";
        }

       latLng = "The location has changed to :" +latLng;
       System.out.println("Listen-Seat " + latLng);
}

    @Override
    public void onDestroy() {
        manager.removeUpdates(locationListener);
        stopSelf();
        System.out.println("关闭位置监听");
    }
}

这样,婚恋系统源码就能实现用户定位了。

声明:本文由云豹科技转发自Grey Sun博客,如有侵权请联系作者删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值