java - 如何在Google Map中设置几个特定位置?

标签 java android google-maps dictionary location

我想为德黑兰的自行车站制作一个 Android 应用程序。为此,我需要在 map 上显示特定的静态位置,如下所示:

google map locations example

我尝试了 Github 上的几个库,但没有成功。我还添加了一个额外的位置 (double latitude1 = 35.747394; double longitude1 = 51.267577;),但位置标记没有出现在 map 上。

如果有人能帮助我,我将不胜感激。

这是我的代码:

MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import com.google.android.gms.maps.MapFragment;
import com.playpersia.bicyclemap.R;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;



public class MapsActivity extends FragmentActivity {

    double latitude1 = 35.747394;
    double longitude1 = 51.267577;
    // Google Map
    private GoogleMap googleMap;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        Uri gmmIntentUri = Uri.parse("geo:35.694677, 51.394871");
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        mapIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //***Change Here***
        mapIntent.setPackage("com.google.android.apps.maps");

        if (mapIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(mapIntent);
            finish();

        }

        try {

            initilizeMap();


            MarkerOptions markerOptions1 = new MarkerOptions().position(
                new LatLng(latitude1, longitude1)).title("بوستان جوانمردان");

            googleMap.addMarker(markerOptions1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @
    Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    private void initilizeMap() {

        if (googleMap == null) {
            SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                R.id.map));

            if (googleMap != null) {
                Toast.makeText(getApplicationContext(), "خطا", Toast.LENGTH_LONG).show();
            }


        }

    }

}

最佳答案

检查Google Places API文档。它允许您查询各种类别的地点信息,例如:机构、著名的兴趣点、地理位置等等。您可以通过邻近度或文本字符串搜索地点。

使用Nearby Search它允许您搜索指定区域内的地点。您可以通过提供关键字或指定您要搜索的地点类型来优化您的搜索请求。这是list of supported types .

Sample HTTP URL of the following form:

https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters

where output may be either of the following values:

  • json (recommended) indicates output in JavaScript Object Notation (JSON)
  • xml indicates output as XML

以下示例是对澳大利亚悉尼某点 500m 半径范围内类型为“transit_station”的地点的搜索请求:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=transit_station&key=YOUR_API_KEY

检查这些相关链接和 tutorial :

希望这有帮助!

关于java - 如何在Google Map中设置几个特定位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38544706/

相关文章:

java - 库存 ClickEvent 不工作

Java 初学者

android - 连接并创建一个 android 应用程序以从 MSSQL 检索数据

javascript - ngModel 在 Controller 中更新,但不在 View 中更新, Angular 2

java - 在java代码中找不到Android资源文件

java - `public class` 和只是 `class` 有什么区别?

Android重新定位 View ,错误的y值

java - 无法从 Foursquare 获取附近的 field

java - 不推荐使用 Android setOnMyLocationChangeListener

ios - 如何更改集群的图标