亚洲国产爱久久全部精品_日韩有码在线播放_国产欧美在线观看_中文字幕不卡在线观看

Android谷歌地圖使用地理編碼器搜索位置含代碼

Android谷歌地圖使用地理編碼器搜索位置

在之前的Android谷歌地圖和Android谷歌地圖顯示當(dāng)前位置的教程中,我們分別介紹了顯示基本谷歌地圖和當(dāng)前位置的功能。愛掏網(wǎng) - it200.com

現(xiàn)在,在這個(gè)教程中我們將實(shí)現(xiàn)谷歌地圖的位置搜索功能。愛掏網(wǎng) - it200.com

通過谷歌地圖API搜索位置是通過 Geocoder 類來完成的。愛掏網(wǎng) - it200.com Geocoder類用于處理 地理編碼 和 逆地理編碼 。愛掏網(wǎng) - it200.com

地理編碼是將街道地址轉(zhuǎn)換為坐標(biāo)(緯度,經(jīng)度)的過程。愛掏網(wǎng) - it200.com逆地理編碼是將坐標(biāo)(緯度,經(jīng)度)轉(zhuǎn)換為地址的過程。愛掏網(wǎng) - it200.com

  1. **List <Address> getFromLocation(double latitude, double longitude, int maxResults): ** 此方法返回一個(gè)指定周圍緯度和經(jīng)度的Address數(shù)組。愛掏網(wǎng) - it200.com
  2. **List <Address> getFromLocationName(String location, int results, double leftLatitude, double leftLongitude, double rightLatitude, double rightLongitude): ** 此方法返回一個(gè)描述給定位置(如地點(diǎn)、地址等)的Address數(shù)組。愛掏網(wǎng) - it200.com
  3. **List <Address> getFromLocationName(String location, int results): ** 此方法返回一個(gè)描述給定位置(如地點(diǎn)、地址等)的Address數(shù)組。愛掏網(wǎng) - it200.com
  4. static boolean isPresent(): 此方法如果實(shí)現(xiàn)了getFromLocation()和 getFromLocationName()方法,則返回true。愛掏網(wǎng) - it200.com

讓我們來看一下將位置名稱轉(zhuǎn)換為坐標(biāo)的代碼。愛掏網(wǎng) - it200.com

List<Address> addressList = geocoder.getFromLocationName(location, 1);
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());

例子:Android谷歌地圖API搜索位置

讓我們看一個(gè)搜索輸入位置的谷歌地圖的例子。愛掏網(wǎng) - it200.com

activity_maps.xml

在activity_maps.xml文件中添加一個(gè)片段(SupportMapFragment)、一個(gè)EditText和一個(gè)按鈕。愛掏網(wǎng) - it200.com

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="example.com.mapexample.MapsActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:layout_width="248dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_weight="0.5"
            android:inputType="textPersonName"
            android:hint="Search Location" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:onClick="searchLocation"
            android:text="Search" />

    </LinearLayout>

</fragment>

build.gradel

在 build.gradel 文件中添加以下依賴項(xiàng)。愛掏網(wǎng) - it200.com

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.google.android.gms:play-services-maps:11.8.0'
    compile 'com.google.android.gms:play-services-location:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

}

MapsActivity.java

在MapsActivity.java文件中添加以下代碼。愛掏網(wǎng) - it200.com

package example.com.mapexample;

import android.location.Address;
import android.location.Geocoder;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.location.LocationServices;

import android.location.Location;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

import java.io.IOException;
import java.util.List;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
        LocationListener,GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener{

    private GoogleMap mMap;
    Location mLastLocation;
    Marker mCurrLocationMarker;
    GoogleApiClient mGoogleApiClient;
    LocationRequest mLocationRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
                mMap.setMyLocationEnabled(true);
            }
        }
        else {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }

    }
    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
        mGoogleApiClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }
        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        mCurrLocationMarker = mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

        //stop location updates
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    public void searchLocation(View view) {
        EditText locationSearch = (EditText) findViewById(R.id.editText);
        String location = locationSearch.getText().toString();
        List<Address> addressList = null;

        if (location != null || !location.equals("")) {
            Geocoder geocoder = new Geocoder(this);
            try {
                addressList = geocoder.getFromLocationName(location, 1);

            } catch (IOException e) {
                e.printStackTrace();
            }
            Address address = addressList.get(0);
            LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
            mMap.addMarker(new MarkerOptions().position(latLng).title(location));
            mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            Toast.makeText(getApplicationContext(),address.getLatitude()+" "+address.getLongitude(),Toast.LENGTH_LONG).show();
        }
    }

}

在AndroidManifest.xml中需要添加的權(quán)限

在AndroidManifest.xml文件中添加以下用戶權(quán)限。愛掏網(wǎng) - it200.com

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

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.com.mapexample">
    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

輸出

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評(píng)論
更多 網(wǎng)友評(píng)論0 條評(píng)論)
暫無評(píng)論

返回頂部

亚洲国产爱久久全部精品_日韩有码在线播放_国产欧美在线观看_中文字幕不卡在线观看

    
    

    久久三级福利| 羞羞色国产精品| 国产精品永久免费| 一区二区三区四区五区精品视频| 欧美日韩视频一区二区| 在线成人av网站| 狂野欧美一区| 在线观看日韩av| 另类av导航| 亚洲欧美日韩国产综合| 亚洲国产精品久久精品怡红院| 国产精品素人视频| 亚洲免费在线视频| 91久久国产综合久久| 国产一级久久| 国产精品日本精品| 国产精品hd| 欧美高清一区| 欧美激情精品| 免费日韩av| 一区二区三区四区五区精品| 伊人狠狠色丁香综合尤物| 久久在线免费观看视频| 欧美在线视频免费观看| 亚洲一区二区久久| 亚洲影院在线| 亚洲大片av| 欧美日韩在线一二三| 午夜精品一区二区在线观看| 欧美精品久久一区二区| 久久精品五月| 久久精品国产综合精品| 久久精品国产亚洲一区二区三区| 欧美在线网站| 亚洲国产高潮在线观看| 欧美日韩高清在线一区| 欧美日韩国产在线一区| 欧美色欧美亚洲另类七区| 亚洲一区二区三区在线| 狠狠v欧美v日韩v亚洲ⅴ| 欧美成人午夜激情在线| 亚洲一区久久久| 亚洲成色777777女色窝| 国产精品国产a级| 国产精品盗摄一区二区三区| 老司机精品导航| 亚洲欧美日韩国产一区| 亚洲精品一二区| 韩日视频一区| 亚洲国产岛国毛片在线| 国产婷婷一区二区| 好看的日韩视频| 国产精品一区=区| 欧美久久精品午夜青青大伊人| 亚洲欧美久久久久一区二区三区| 亚洲嫩草精品久久| 在线成人黄色| 一区二区三区日韩精品| 欧美影片第一页| 欧美精品成人一区二区在线观看| 欧美日韩直播| 欧美激情第3页| 欧美激情1区| 噜噜噜91成人网| 久久成人综合网| 亚洲欧美日韩精品久久奇米色影视| 亚洲国内自拍| 在线观看亚洲精品视频| 国产午夜精品一区理论片飘花| 国产精品国产精品| 在线观看欧美一区| 国内欧美视频一区二区| 99视频超级精品| 浪潮色综合久久天堂| 国产精品白丝黑袜喷水久久久| 在线观看视频一区二区| 亚洲欧美偷拍卡通变态| 亚洲尤物在线视频观看| 欧美激情成人在线| 狠久久av成人天堂| 欧美亚洲综合网| 国产精品vvv| 国产精品理论片在线观看| 亚洲精品国产精品国自产观看浪潮| 性久久久久久久久久久久| 欧美日韩一区二区三区高清| 激情综合视频| 久久精品九九| 麻豆国产va免费精品高清在线| 美女视频网站黄色亚洲| 国产亚洲欧美日韩日本| 亚洲影视综合| 国产精品视频| 亚洲夜晚福利在线观看| 欧美日韩一区综合| 夜色激情一区二区| 欧美日本韩国一区| 91久久在线| 欧美jizzhd精品欧美喷水| 欧美激情视频一区二区三区在线播放 | 欧美一区午夜精品| 久久精品五月| 欧美成人午夜77777| 亚洲欧洲精品一区二区精品久久久 | 红桃视频国产一区| 久久综合九色综合久99| 亚洲第一视频网站| 欧美va天堂| 99精品视频免费观看视频| 一区二区三区高清| 国产精品久久久久久久久久免费 | 日韩一级黄色大片| 欧美在线日韩精品| 欧美激情视频一区二区三区免费 | 91久久精品国产91久久性色| 一区二区三区色| 久久久久国产成人精品亚洲午夜| 国产亚洲欧美另类一区二区三区| 亚洲国产欧美在线| 性欧美大战久久久久久久免费观看| 久久久久久久久一区二区| 国产一区亚洲一区| 亚洲人永久免费| 久久精品人人做人人爽电影蜜月 | 久久视频免费观看| 亚洲另类一区二区| 欧美体内谢she精2性欧美| 好看的日韩视频| 欧美极品aⅴ影院| 中文欧美日韩| 精品96久久久久久中文字幕无| 一区二区三区自拍| 免费成人你懂的| 欧美日韩综合另类| 久久九九热免费视频| 亚洲黄色在线看| 久久这里有精品15一区二区三区| 亚洲人成绝费网站色www| 国产精品久久久久天堂| 久久久午夜精品| 亚洲午夜激情网页| 欧美婷婷久久| 亚洲神马久久| 亚洲激情视频网站| 欧美高清在线视频观看不卡| 国产欧美三级| 欧美在线免费观看| 亚洲无线一线二线三线区别av| 精品999在线观看| 久久精品电影| 尤物精品在线| 国产午夜精品一区二区三区视频 | 欧美精品久久天天躁 | 亚洲国产婷婷香蕉久久久久久99| 久热精品视频| 国产精品高潮在线| 欧美精品在线观看播放| 免费人成网站在线观看欧美高清| 午夜久久久久久久久久一区二区| 一本色道久久88综合亚洲精品ⅰ| 久久综合电影一区| 狠狠综合久久| 欧美第十八页| 亚洲激情av在线| 亚洲二区在线| 国产精品极品美女粉嫩高清在线 | 亚洲欧美日韩爽爽影院| 99这里只有精品| 99在线精品视频| 亚洲视频播放| 欧美图区在线视频| 午夜视频精品| 久久本道综合色狠狠五月| 午夜国产精品视频免费体验区| 国产欧美日韩不卡| 久久综合狠狠| 欧美国产第一页| 欧美日本国产| 国产精品久久久久久久午夜| 久久精品国产999大香线蕉| 欧美综合77777色婷婷| 亚洲国语精品自产拍在线观看| 欧美日韩免费在线视频| 欧美视频在线一区| 国产精品久久综合| 欧美jizzhd精品欧美巨大免费| 欧美高清视频在线| 国产精品a久久久久久| 国产精品福利网| 久久男人资源视频| 亚洲欧美另类在线| 亚洲高清在线| 在线亚洲精品| 亚洲福利视频二区| 夜夜狂射影院欧美极品| 国内久久精品| 亚洲精美视频| ●精品国产综合乱码久久久久| 国产精品成人久久久久| 国产日韩视频一区二区三区|